Set default value for cut off date and ballot date using value of date field in the same model in django
Question: I have created a model for entering sitting date of a session along with cut
off date and ballot date. My model is:

    
    
    from datetime import datetime, timedelta
    class Sitting(models.Model):
        sit_date = models.DateField(blank=False)
        cut_off_date = models.DateField(default=get_cut_off_date)
        ballot_date = models.DateField(default=ballot_date)
        genre = TreeForeignKey('Genre', null=True, blank=True, db_index=True)
        sess_no = models.ForeignKey(Session,
                                     on_delete=models.CASCADE)
    
        def get_cut_off_date(self):
            return self.sit_date - timedelta(days=16)
    
        def ballot_date(self):
            return self.sit_date - timedelta(days=12)
    
        def __str__(self):  # __unicode__ on Python 2
            return self.sit_date
    

I want to set cut off date and ballot date default values from the values of
sitting date of the same model. But my model not work. How to set the default
values of cut off date and ballot date from the input of sitting date?


Answer: Well you have created the function to get cut_off_date so just override the
save method to update the `cut_off_date`

    
    
    class Sitting(models.Model):
        sit_date = models.DateField(blank=False)
        cut_off_date = models.DateField(null=True, blank=True)
    
        def get_cut_off_date(self):
            ....
    
        def save(self,  *args, **kwargs):
            self.cut_off_date = self.get_cut_off_date()
            super(Model, self).save(*args, **kwargs)
    

