File: utils.py

package info (click to toggle)
python-django-timezone-field 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: python: 1,047; sh: 6; makefile: 3
file content (19 lines) | stat: -rw-r--r-- 725 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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