SunGoKu
SunGoKu

Reputation: 11

How to Set DateTimeFields in Django Models to None Value (If current time is greater than this fields with auto detect)

Below is my codes

class Job(models.Model):
      pin_till = models.DateTimeField("Pin Job Post", null=True,blank=True)

Here i wanna auto setup pin_till_date field to None if date is not None and than pin_till_date < now(). Depend on current time and auto check this field to None.
How to archives this method ah? with @property function, signals or just define in save function?

Upvotes: 1

Views: 83

Answers (1)

Stack User 5674
Stack User 5674

Reputation: 1588

You can Override Save()

class Job(models.Model):
    def save(self, *args, **kwargs):
        if self.pin_till < now(): # Or what ever be the condition
        # Use your conditon and update 
           self.pin_till = None 
    super(Job, self).save(*args, **kwargs) #Save the modified value

Upvotes: 1

Related Questions