django-extra-checks
django-extra-checks copied to clipboard
Forbid `datetime.now` and `timezone.now` in default
People use timezone.now
(sometimes even timezone.now()
) as a default value.
But, there are auto_now_add=True
and auto_now=True
for DateTimeField
.
Sub-checks:
-
timezone.now()
is evaluated once, usetimezone.now
- Don't use
datetime.now
, it does not have a proper timezone support
timezone.now() is evaluated once, use timezone.now
this case is checked by fields.W161 https://docs.djangoproject.com/en/3.1/ref/checks/#model-fields
But, there are auto_now_add=True and auto_now=True for DateTimeField
auto keywords and default=timezone.now
yield different results, so I'm not sure that we should have opinion about that.
Don't use datetime.now, it does not have a proper timezone support
that one looks like a good candidate for check