django-extra-checks icon indicating copy to clipboard operation
django-extra-checks copied to clipboard

Forbid `datetime.now` and `timezone.now` in default

Open sobolevn opened this issue 3 years ago • 1 comments

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, use timezone.now
  • Don't use datetime.now, it does not have a proper timezone support

sobolevn avatar Oct 30 '20 20:10 sobolevn

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

kalekseev avatar Oct 30 '20 20:10 kalekseev