-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert TimeZoneField string value to timezone object on assignment
Add a descriptor_class that deserializes a string TimeZoneField value to a timezone object (pytz timezone or zoneinfo depending on settings) when it is assigned. As a side effect, invalid (non-blank) timezone names are detected immediately (rather than at save/full_clean time), and will immediately raise a ValidationError. Closes #57
- Loading branch information
Showing
4 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.db.models.query_utils import DeferredAttribute | ||
|
||
|
||
class AutoDeserializedAttribute(DeferredAttribute): | ||
""" | ||
Use as the descriptor_class for a Django custom field. | ||
Allows setting the field to a serialized (typically string) value, | ||
and immediately reflecting that as the deserialized `to_python` value. | ||
(This requires that the field's `to_python` returns the same thing | ||
whether called with a serialized or deserialized value.) | ||
""" | ||
|
||
# (Adapted from django.db.models.fields.subclassing.Creator, | ||
# which was included in Django 1.8 and earlier.) | ||
|
||
def __set__(self, instance, value): | ||
value = self.field.to_python(value) | ||
instance.__dict__[self.field.attname] = value |