#Restframework serialization filter

5 messages · Page 1 of 1 (latest)

nova ermine
#

In DRF, when serializing the customer object, I want to return only customergroups which meet a certain condition like names present in a set or based on the customer who requests this customer object via api, we only want to show the customer groups for which he has access to, instead of returning all customergroups of a customer. How to do this properly?

balmy totem
#

This is normally done in a View or Viewset, where a QuerySet is created and passed to a Serializer to do the work

nova ermine
#

query set is hard to get without saving it, right?

#

For ex:- Customer.objects.get(pk=<id>).customergroup_set.filter(pk__in=<set of group ids>) --> qs of groups to be shown, but you don't want to set it to customer object & return it to serializer.

#

qs of customer group is constructed in customer serializer & serialized via customer group serializer & returned in api response.