Opened 7 years ago

Closed 7 years ago

#29028 closed Cleanup/optimization (invalid)

Warning show up even use `django.utils.timezone.now`

Reported by: Windson yang Owned by: nobody
Component: Uncategorized Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Create a new project like usual and create a model like this

from django.db import models
from django.utils import timezone

class Learn(models.Model):
    title = models.CharField(max_length=50)
    pay_time = models.DateTimeField(default=timezone.now())

after running migrations and run server, warning still show up
learn.Learn.pay_time: (fields.W161) Fixed default value provided.

HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use django.utils.timezone.now

This model already use django.utils.timezone.now, so I think we should not show this warning.

code on https://github.com/django/django/blob/d7b2aa24f75434c2ce50100cfef3586071e0747a/django/db/models/fields/__init__.py#L1187

Change History (4)

comment:1 by Windson yang, 7 years ago

Type: UncategorizedCleanup/optimization

comment:2 by Tim Graham, 7 years ago

I guess you didn't understand the warning. You should use timezone.now (without parentheses) to use the time the object was created, otherwise timezone.now() evaluates to the time when the web server loads the models.py file. Should the message be clarified?

comment:3 by Windson yang, 7 years ago

Thank you, it's my mistake. :D

comment:4 by Mariusz Felisiak, 7 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top