Opened 13 years ago

Closed 13 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: dev
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 13 years ago.
17294.patch (2.4 KB ) - added by Aymeric Augustin 13 years ago.

Download all attachments as: .zip

Change History (8)

by Daniel Swarbrick, 13 years ago

Attachment: timezone.py.patch added

comment:1 by Aymeric Augustin, 13 years ago

Owner: changed from nobody to Aymeric Augustin

comment:2 by Aymeric Augustin, 13 years ago

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 by Daniel Swarbrick, 13 years ago

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 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

Thank for the clarification.

comment:5 by Daniel Swarbrick, 13 years ago

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').

by Aymeric Augustin, 13 years ago

Attachment: 17294.patch added

comment:6 by Aymeric Augustin, 13 years ago

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