'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?
Solution 1:[1]
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
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 |
