Reputation: 1766
When I call User.create_user
, I get the following error:
IntegrityError at /login
users_user.status_id may not be NULL
Note: I have looked at a bunch of other IntegrityError questions on SO, and I couldn't fix this problem.
My models look like this:
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()
I also still don't understand what User.status_id represents. Why is it there, and why can it not be None
?
Upvotes: 1
Views: 217
Reputation: 62948
Your problem is that you save your user with an uncreated status. Create a status first, then you can save the user:
def create_user(cls, fid, friends):
k = User()
k.status = Status()
k.status.set_status('') # set_status saves the status
k.facebook_id = fid
k.friends = friends
k.save()
Or better yet:
def create_user(cls, fid, friends):
status = Status()
status.set_status('')
User.objects.create(facebook_id=fid, friends=friends, status=status)
In idiomatic django code, methods like create_user
should be on the models' manager:
class UserManager(models.Manager):
def create_user(self, fid, friends):
status = Status()
status.set_status('')
return self.create(facebook_id=fid, friends=friends, status=status)
class User(models.Model):
facebook_id = models.CharField(max_length=200)
friends = ListField()
status = models.OneToOneField(Status)
objects = UserManager()
Then you call it like this:
new_user = User.objects.create_user(fid, friends)
Then you notice you could've just used the builtin objects.create
, if only there were a default status:
# A custom manager is no longer needed
class User(models.Model):
facebook_id = models.CharField(max_length=200)
friends = ListField()
status = models.OneToOneField(Status)
def save(self, *a, **kw):
# if this user doesn't have a status yet, create an empty one
if self.status is None:
self.status = Status()
self.status.set_status('')
super(User, self).save(*a, **kw)
# creating users:
new_user = User.objects.create(facebook_id=fid, friends=friends)
# status is set to an empty one automatically in save
Upvotes: 3