[Fixed]-Calculate DateField until days in Django

1๐Ÿ‘

โœ…

Add a custom save method to your model class. Sample code below will automatically compute expiry date only upon creation. Subsequent modification of the record will not trigger this automatic computation.

class ModelName(models.Model):
    created = models.DateTimeField()
    expired = models.DateTimeField(blank=True, null=True)

    def save(self, *args, **kwargs):
        if not self.pk:
            self.expired = self.created + datetime.timedelta(days=30)
        super(ModelName, self).save(*args, **kwargs)
๐Ÿ‘คWannabe Coder

0๐Ÿ‘

if u want to use the solution above, it better for your code to use signals:
https://docs.djangoproject.com/en/1.8/ref/signals/#post-save

๐Ÿ‘คHetdev

Leave a comment