Opened 5 years ago

Closed 5 years ago

#17294 closed Bug (fixed)

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

Reported by: pressureman Owned by: aaugustin
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


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) (994 bytes) - added by pressureman 5 years ago.
17294.patch (2.4 KB) - added by aaugustin 5 years ago.

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by pressureman

comment:1 Changed 5 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to aaugustin
  • Patch needs improvement unset

comment:2 Changed 5 years ago by aaugustin

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 pressureman

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 aaugustin

  • Triage Stage changed from Unreviewed to Accepted

Thank for the clarification.

comment:5 Changed 5 years ago by pressureman

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 aaugustin

comment:6 Changed 5 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

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