Setting a default value for model fields in Django

116 Views Asked by At

I want to create a model in Django with two fields {initial price, current price} .. is there a way in Django to set the default value for the current price field to be the same as the initial price field value?

1

There are 1 best solutions below

2
On

Not sure why you would have 2 price fields with one as initial and another.

But a possible workaround for that would be to override the save() method of the model to set the initial value for the model's current price.

e.g:

def save(self, *args, **kwargs):
    if not self.pk:
        self.current_price = self.initial_price
    super(Model, self).save(*args, **kwargs)

Of course the above is just an example and is not an exact/specific answer, but I think that's a good starting point.