user3631116
user3631116

Reputation:

Django save method fails silently

I have a model something like this

class UserAttributes(models.Model):

uuid                = models.CharField(max_length=63, null=True,blank=True)
user                = models.OneToOneField(User)
activation_date     = models.DateTimeField(null=True, blank=True)
mobile_number       = models.CharField(max_length=20, null=True, blank=True)
created_date        = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return '{0} - {1}'.format(self.user.id, self.user.get_full_name())

def save(self, *args, **kwargs):

    if not self.pk:
        self.uuid = uuid.uuid4().hex
        return super(UserAttributes, self).save(*args, **kwargs)

I have overridden save method. But, in my views when I manually do something like

attributes.activation_date = timezone.now()
attributes.save()

the value for field activation_date is never written to database. This is the first time I am facing such issue. No exceptions are raised. Can someone point out as to what might exactly be the case here.

Upvotes: 2

Views: 1239

Answers (1)

hspandher
hspandher

Reputation: 16743

I have noticed that, in some conditions, you are not calling super in model save. It should have been like this

def save(self, *args, **kwargs):

    if not self.pk:
        self.uuid = uuid.uuid4().hex
    return super(UserAttributes, self).save(*args, **kwargs)

So in case, when object is updated save is not called, therefore data is not updated.

Upvotes: 6

Related Questions