当我打电话时User.create_user
,我收到以下错误:
IntegrityError at /login
users_user.status_id may not be NULL
注意:我查看了其他关于 SO 的 IntegrityError 问题,但无法解决此问题。
我的模型如下所示:
class Status(models.Model):
status = models.CharField(max_length=200)
status_time = models.DateTimeField('date published')
def __init__(self, status = '', time=timezone.datetime.min, *args, **kwargs):
self.status = status
self.status_time = time
super(Status, self).__init__(*args, **kwargs)
def set_status(self, s):
self.status = s
self.status_time = timezone.datetime.now()
self.save()
def get_status(self):
now = timezone.datetime.now()
if now-self.status_time<datetime.timedelta(minutes=15):
return self.status
return None
class User(models.Model):
facebook_id = models.CharField(max_length=200)
friends = ListField()
status = models.OneToOneField(Status)
@classmethod
def create_user(cls, fid, friends):
k = User()
k.facebook_id = fid
k.friends = friends
k.save() #THE PROBLEM IS HERE
k.status = Status()
k.status.set_status('')
k.status.save()
k.save()
我也不明白 User.status_id 代表什么。为什么它在那里,为什么它不在那里None
?