Ethan
Ethan

Reputation: 747

Overriding Django auto_now in datefiled

Is there a way to pass a date to datafield that overrides auto_now? I want to only use auto_now if a date is not passed.

Upvotes: 2

Views: 1026

Answers (1)

Basalex
Basalex

Reputation: 1187

According to the docs:

Note that the current date is always used; it’s not just a default value that you can override. https://docs.djangoproject.com/en/2.0/ref/models/fields/#datefield

So just don't use auto_now, use default, for example:

from django.utils import timezone

class YourModel(models.Model):
    date_approved = models.DateField(default=timezone.now)

Upvotes: 4

Related Questions