Opened 5 years ago

Closed 5 years ago

#17294 closed Bug (fixed)

django.utils.timezone functions raise AttributeError if DateTimeField allows null

Reported by: Daniel Swarbrick Owned by: Aymeric Augustin
Component: Internationalization Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Since a null DateTimeField == None, and Python's None type does not contain a tzinfo attribute, the functions is_aware() and is_naive() will raise AttributeError.

Attachments (2)

timezone.py.patch (994 bytes) - added by Daniel Swarbrick 5 years ago.
17294.patch (2.4 KB) - added by Aymeric Augustin 5 years ago.

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by Daniel Swarbrick

Attachment: timezone.py.patch added

comment:1 Changed 5 years ago by Aymeric Augustin

Owner: changed from nobody to Aymeric Augustin

comment:2 Changed 5 years ago by Aymeric Augustin

By design, these functions expect to receive a datetime object and don't check the type of their argument. It's the caller's job to ensure that they aren't called with None, or a string, etc.

If I understand correctly, creating a model with a nullable DateTimeField and saving it without a value in the admin should trigger the problem?

comment:3 Changed 5 years ago by Daniel Swarbrick

Correct. My model looks like this:

class Device(models.Model):
   ...
   last_poll = models.DateTimeField(blank=True, null=True)
   ...

When a Device object is created, it may have the value None in the last_poll field, to be populated at some later stage.

comment:4 Changed 5 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

Thank for the clarification.

comment:5 Changed 5 years ago by Daniel Swarbrick

Maybe instead of checking for a non-None value being passed, it might be more prudent to check isinstance(value, datetime) - or perhaps hasattr('tzinfo').

Changed 5 years ago by Aymeric Augustin

Attachment: 17294.patch added

comment:6 Changed 5 years ago by Aymeric Augustin

Resolution: fixed
Status: newclosed

In [17148]:

Fixed #17294 -- Supported nullable DateTimeFields when time zone support is enabled. Thanks pressureman for the report.

Note: See TracTickets for help on using tickets.
Back to Top