简体   繁体   English

Django用户表为外键

[英]Django User table as Foreign Key

I'm a new user to Django and am just starting my first app, "myapp". 我是Django的新用户,我刚刚开始使用我的第一个应用程序“myapp”。

In my models.py for my app I have the following model: 在我的models.py中,我的应用程序有以下型号:

class Subcalendar(models.Model):
  user = models.ForeignKey(User)

But when I try running: 但是当我尝试跑步时:

python manage.py sql myapp

I get an error stating: 我收到一个错误说明:

NameError: name 'User' is not defined

Do I need to make my own model for User or is there another way of calling it in my model's foreign key field? 我是否需要为User创建自己的模型,或者是否有另一种方法在我的模型的外键字段中调用它?

You need import the User model at the top of your file 您需要导入文件顶部的User模型

 from django.contrib.auth.models import User

so python can resolve the reference. 所以python可以解析引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM