[Fixed]-How do I make a custom model Field call to_python when the field is accessed immediately after initialization (not loaded from DB) in Django >=1.10?


After lots of digging it turns out that in 1.8 the behaviour of custom fields was changed in such a way that to_python is no longer called on assignment to a field.


The new approach doesn’t call the to_python() method on assignment as was the case with SubfieldBase. If you need that behavior, reimplement the Creator class from Django’s source code in your project.

Here’s a Django ticket with some more discussion on this change: https://code.djangoproject.com/ticket/26807

So in order to retain the old behaviour you need to do something like this:

class CastOnAssignDescriptor(object):
    A property descriptor which ensures that `field.to_python()` is called on _every_ assignment to the field.
    This used to be provided by the `django.db.models.subclassing.Creator` class, which in turn
    was used by the deprecated-in-Django-1.10 `SubfieldBase` class, hence the reimplementation here.

    def __init__(self, field):
        self.field = field

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        return obj.__dict__[self.field.name]

    def __set__(self, obj, value):
        obj.__dict__[self.field.name] = self.field.to_python(value)

And then add this to the custom field:

def contribute_to_class(self, cls, name):
    super(MyField, self).contribute_to_class(cls, name)
    setattr(cls, name, CastOnAssignDescriptor(self))

Solution was taken from this pull request: https://github.com/hzdg/django-enumfields/pull/61


Leave a comment