django - How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead." -
on upgrade django 1.9, warning
removedindjango110warning: subfieldbase has been deprecated. use field.from_db_value instead.
i see problem arises. have custom field definitions, , in them have __metaclass__ = models.subfieldbase
. example,
class durationfield(models.floatfield): __metaclass__ = models.subfieldbase def __init__(self, *args, **kwargs): ...
if __metaclass__
statement deprecated, supposed replace exactly?
do take out , add from_db_value
method in example here: https://docs.djangoproject.com/en/1.9/howto/custom-model-fields/#converting-values-to-python-objects ?
and how from_db_value
, to_python
different? both seem convert database data python objects?
yes, should remove __metaclass__
line , add from_db_value()
, to_python()
:
class durationfield(models.floatfield): def __init__(self, *args, **kwargs): ... def from_db_value(self, value, expression, connection, context): ... def to_python(self, value): ...
as described here: https://docs.djangoproject.com/en/1.9/ref/models/fields/#field-api-reference, to_python(value)
converts value (can none, string or object) correct python object.
from_db_value(value, expression, connection, context)
converts value returned database python object.
so, both methods return python objects, used django in different situations. to_python()
called deserialization , during clean()
method used forms. from_db_value()
called when data loaded database
Comments
Post a Comment