django:尝试保存时必须是用户实例,但它是! [英] django: must be a User Instance when trying to save, but it is!
问题描述
我确定这可能是一个简单的答案,但我看不到任何错误!
我只是想保存一个记录,而它的说法无法分配< User:jason.kinnear>:Confirmation.user必须是User实例。
这是代码:
u = User.objects.get(pk = 18)
cr =确认( details ='...',user = u).save()
提前感谢
编辑:
确认模型定义
类确认( model.Model):
number = models.IntegerField(null = True,blank = True)
user = models.ForeignKey('User')
details = models.CharField(max_length = 250 )
time = models.DateTimeField(auto_now_add = True)
p>
user = models.ForeignKey('User')
和
user = models.Fo reignKey(User)
我不太确定这个区别是诚实的
编辑2:
用户类已经如下扩展:
from django .contrib.auth.models import User as BaseUser
class User(BaseUser):
额外的用户信息
location = models.ForeignKey('Location',null = True ,blank = True)
defaults = models.CharField(null = True,blank = True)
只是这个问题有一个答案,Ignacio是正确的(在评论中)。导入的用户
不一样。 Django 用户
被扩展为另一个用户
类,我正在导入错误的一个。 (Ignacio,如果你想发表一个答案,我会做出接受的答案)
Im sure this is probably a simple answer, but I cannot see anything wrong with this!
Im just trying to save a record, and its saying Cannot assign "<User: jason.kinnear>": "Confirmation.user" must be a "User" instance.
This is the code:
u = User.objects.get(pk=18)
cr = Confirmation(details='...', user=u).save()
Thanks in advance
EDIT: Model Definition for Confirmation
class Confirmation(models.Model):
number = models.IntegerField(null=True, blank=True)
user = models.ForeignKey('User')
details = models.CharField(max_length=250)
time = models.DateTimeField(auto_now_add=True)
I have tried both
user = models.ForeignKey('User')
and
user = models.ForeignKey(User)
Im not too sure of the difference to be honest
EDIT 2: The User class has been extended like this:
from django.contrib.auth.models import User as BaseUser
class User(BaseUser):
"""Extra user information"""
location = models.ForeignKey('Location', null=True, blank=True)
defaults = models.CharField(null=True, blank=True)
Just so this question has an answer, Ignacio was correct (in the comments). The imported User
was not the same. The Django User
was extended with another User
class and I was importing the wrong one. (Ignacio, if you would like to post an answer I will make that the accepted answer)
这篇关于django:尝试保存时必须是用户实例,但它是!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!