'How can I define a django model's field default value to be another field value?

In my model I have these two fields:

start_bid = models.IntegerField(default=0)
max_bid =  models.IntegerField(default="the start_bid value")

The start_bid will be added by the user once through Django ModelForm. And the max_bid may change and may not. So I need the first value of 'max_bid' to be the start_bid value entered by the user, then if it changes it will be updated.

In brief, I need the initial value of max_bid to be equal to the start_bid!

Any suggestions?



Solution 1:[1]

Here is the solution:

class Bid(models.Model):
          start_bid = models.IntegerField(default=0)
          max_bid =  models.IntegerField()
          
          def save(self,*args,**kwargs): 
               if not self.max_bid:
                   self.max_bid = self.start_bid   
                
               super(Bid,self).save(*args,**kwargs) #here Bid is my model name. Don't forget to replace Bid with your model name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 boyenec