4👍
✅
The problem is that you’re not saving the changes to your database.
It works in the terminal because that particular model instance (the python object – very much temporary) has the property identification
filled. When you access it in a view or template, the save()
method has not been called so the property / field is blank.
To make it work, call save again after your first save. Also, it might make sense to set the id only on model creation. One extra call per initial save isn’t so big of a deal in most cases.
def save(self, *args, **kwargs):
add = not self.pk
super(MyModel, self).save(*args, **kwargs)
if add:
self.identification = str(self.id)
kwargs['force_insert'] = False # create() uses this, which causes error.
super(MyModel, self).save(*args, **kwargs)
Source:stackexchange.com