Hi Matus!

> Is there any replacement to this / what is the best way to do it if I'd 
like
> to call to_python on assignment?

If you'd like to have a field's `to_python()` called on assignement you 
should
attach a descriptor[1] on the model class on `contribute_to_class()` that 
calls
it.

e.g.

class Converter(object):
    def __init__(self, field):
        self.name = field.name
        self.convert = field.to_python

    def __get__(self, instance, owner=None):
        if instance is None:
            return self
        try:
            return instance.__dict__[self.name]
        except KeyError:
            return AttributeError(self.name)

    def __set__(self, instance, value):
        instance.__dict__[self.name] = self.convert(value)


class FieldConverterMixin(object):
    def contribute_to_class(model, name):
        super(FieldConverterMixin, self).contribute_to_class(model, name)
        setattr(model, name, Converter(self))

This is mostly what SubfieldBase use to do.

Cheers,
Simon

Le samedi 5 décembre 2015 12:04:10 UTC-5, matus moravcik a écrit :
>
> Hi, 
>
> according to the docs, SubfieldBase is going to be removed in 1.10 and 
> replaced by from_db_value()
>
> https://docs.djangoproject.com/en/1.9/releases/1.8/#subfieldbase
>
> What puzzles me is this:
> "Note that the new approach does not call the to_python() 
> <https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.Field.to_python>
>  method 
> on assignment as was the case with SubfieldBase."
>
> I thought that was the main point of SubfieldBase. Is there any 
> replacement to this / what is the best way to do it if I'd like to call 
> to_python on assignment?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d23eb3b7-6db8-4e3e-b28a-5db900e26b3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to