Closed JoelBennett closed 7 years ago
There's not a great way to do this on the field itself, but you could override the to_value
method in your custom serializer like this (code not tested but idea should work):
class MySerializer(serpy.Serializer):
def set_defaults(self, v):
for name, _, _, _, _, _ in self._compiled_fields:
v.setdefault(name)
return v
def to_value(self, instance):
v = super(MySerializer, self).to_value(instance)
if self.many:
return map(self.set_defaults, v)
return self.set_defaults(v)
Let me know if that will solve your issue.
That actually worked perfectly. Thanks!
Forgive me if my terminology is a bit off. So far I've been pretty impressed with Serpy - we were able to get some serious performance out of it compared to the Django Rest Framework serializer when returning several thousand objects in a single call (don't ask).
I have run into a small challenge, and I'm not quite sure the best way to overcome it. I've got a Serializer that inherits from serpy.Serializer, which has a number of fields (let's call them a, b, c, d, etc.). These fields may or may not be present in the object which I need to serialize, but I'd always like to make sure that they are present with a value of None in the serialized data. So my serializer looks something like:
Inside of a Django view, the QuerySet of objects is filtered, and passed into the serializer.
This raises the following exception: