Reputation: 1168
I want to know the best way to assign a generated value for model field in a django form. Here is the code for what I need to implement the logic.
Model:
class Booking:
number = models.CharField(max_length=8)
category = models.CharField(max_length=2)
ref = models.CharField(max_length=10)
What I need to do is, store the combination of number + category
in ref
field when model is saved. I know there are two methods called save()
and clean()
available for this. But I'm not sure which one is the best to use.
Thanks.
Upvotes: 0
Views: 44
Reputation: 3611
You can do this with a custom save
function in the Booking
model.
class Booking(models.Model):
...
def save(self, *args, **kwargs):
self.ref = "%s%s" % (self.number, self.category)
super(Booking, self).save(*args, **kwargs)
Upvotes: 1