Le samedi 3 juin 2017 08:44:07 UTC-4, Alexis Roda a écrit :
Hi,
I have a custom ModelField (HorariField, a simple weekly timetable) written for django 1.7 and I'm trying to upgrade it to work with django 1.10, so I have removed the SubfieldBase metaclass and added the from_db_value method. The field inherits from CharField and stores Horari instances that are serialized to/from strings like "mo,tu,we:08:00-14:00", it works like the HandField in the django docs.
After the changes I'm getting errors when serializing to json the models that have that kind of field:
TypeError: <hera_django.lib.horari.Horari object at 0x7fb3f1e619d0> is not JSON serializable
I'm not using the django serializers. The json serialization look like:
json.dumps(list(result), cls=DjangoJSONEncoder)
where result is a ValuesQuerySet.
After some debugging it seems that with the old HorariField class the json serializer already receives a serialized value, so it worked until now, but upgrading the field class the serializer receives an Horari instance.
I'm wondering if I'm missing something, some magic provided by SubfieldBase that I've no replicated in the new class, or this is how it's supposed to work and I need to take care of this in the serializer.
Here's the code for the field:
class HorariField(models.CharField):
description = _(u"Horari")
def __init__(self, *args, **kwargs):
if "max_length" not in kwargs:
kwargs["max_length"] = 188
if "default" not in kwargs:
kwargs["default"] = None
super(HorariField, self).__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
def from_db_value(self, value, expression, connection, context):
if value is None:
return None
return horari.Horari(value)
def to_python(self, value):
if isinstance(value, horari.Horari):
return value
return horari.Horari(value)
def get_prep_value(self, value):
if value is None:
return None
return value.to_str()
def formfield(self, form_class=fields.HorariField, **kwargs):
return super(HorariField, self).formfield(form_class=form_class, **kwargs)
I've tried defining the value_to_string method but I get the same error. The method it's never called. I assume that it's meant for django serializers' consumption.
TIA
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7710ff9d-bee1-40d4-a71f-13f965a4b5b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment