如何在Django中为User表创建一个外键? [英] How do I create a foreign key to the User table in Django?
本文介绍了如何在Django中为User表创建一个外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个名为Tickets的新模型,我想确保始终有一个分配给它的有效用户ID。
I'm creating a new model called Tickets which I want to ensure always has a valid userID assigned to it.
我使用 AUTH_PROFILE_MODULE
设置配置文件时,也会给出错误 NameError:name'User'未定义,当我尝试运行syndb。
I'm using AUTH_PROFILE_MODULE
to setup the profile which also gives the error NameError: name 'User' is not defined when I try to run syndb.
如何设置外键以确保始终如此?
How do I setup a foreign key to make sure this always is the case?
## tickets/models.py
class Ticket(models.Model):
user = models.ForeignKey(User,)
# More model stuff.
# accounts/models.py
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
推荐答案
所以问题是我错过了导入我的模型。
so the problem was I was missing an import on my models.
from django.contrib.auth.models import User
这篇关于如何在Django中为User表创建一个外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文