clarkduvall / serpy

ridiculously fast object serialization
http://serpy.readthedocs.org/en/latest/
MIT License
960 stars 62 forks source link

Patch for compatibility with RelatedManager (reverse foreign-key) #36

Closed davidtgq closed 7 years ago

davidtgq commented 7 years ago

It wasn't working with nested reverse foreign-keys, need to add .all():

def to_value(self, instance):
    fields = self._compiled_fields
    if self.many:
        serialize = self._serialize
        return [serialize(o, fields) for o in instance.all()]
    return self._serialize(instance, fields)

This enables to do something like:

class ParentSerpy(serpy.Serializer):
    children = ChildSerpy(attr='child_set', many=True)
clarkduvall commented 7 years ago

You can do something similar to what is described in #17

children = ChildSerializer(many=True, attr='child_set.all', call=True)

Does that work for you?

davidtgq commented 7 years ago

Yes I tried that and it works, thanks. I did not put call=True before, trying attr='child_set.all()' did not work. Thanks for the help, this is even better.