I’ve been looking at this a few days now and am getting to pullout my hair so any help would be appreciated.
I have a simple model called package that has a userFrom and userTo
class Package(models.Model):
title = models.CharField(max_length=50)
packageid = models.CharField(primary_key=True, max_length=64, default=uuid_str, editable=False)
userfrom = models.ForeignKey('auth.user', editable=False, related_name='userfrom')
userto = models.ForeignKey('auth.user', editable=False, related_name='userto')
and my views for package has the following to assign the mapping
def pre_save(self, obj):
print "adding user to object! %s "%self.request.user
obj.userfrom = self.request.user
Hoever it seems that pre_save isn’t getting called and the mapping of userid’s isn’t happening. When I log in with the api and try to push a new package I get the error.
I’ve tried to map this has closely as possible to the tutorial http://django-rest-framework.org/tutorial/4-authentication-and-permissions.html without any luck.
Exception Type: IntegrityError
Exception Value:
null value in column “userfrom_id” violates not-null constraint
browsing the source to the sql I can see that userfrom_id and userto_id are both null.
Any idea’s much appreciated.
J
I had the same issue…
You have to call the pre_save in your post with the serialized object: