Fails for recursive fields.

#25 · closed · 2 comments

View on GitHub ↗

noodlebreak

This is my serializer: ``` class CategoryRecursiveSerializer(DynamicFieldsMixin, serializers.ModelSerializer): children = serializers.ListField(read_only=True, source='get_children', child=RecursiveField()) value = serializers.CharField(source='name', read_only=True) # hack requested by frontend parent = fields.CategoryField(queryset=models.Category.objects.all(), required=False, allow_null=True) ``` This is my request: ``` http://api.mlocal.com/api/v1/categories/?fields=id,value,children ``` This is my response: ``` { "count": 20, "next": null, "previous": null, "results": [ { "id": "5914220d-c455-45ff-a135-e9ac7f307dae", "value": "Fruits & Vegetables", "children": [ { "id": "7426afb3-b2b6-4f74-8a25-6e5e5865fb4f", "name": "Fresh Vegetables", "value": "Fresh Vegetables", "image": null, "is_featured": false, "children": [], "parent": { "id": "5914220d-c455-45ff-a135-e9ac7f307dae", "name": "Fruits & Vegetables", "image": "http://api.mlocal.com/categories/35629/fruits-vegetables-41698-1523944087.png", "is_featured": true, "parent": null, "created": "2018-07-17T09:27:42.920241Z" }, "created": "2018-07-17T09:28:32.731214Z" } ], "image": "http://api.mlocal.com/categories/35629/fruits-vegetables-41698-1523944087.png" } ] } ``` The `parent` field comes in nested children, except for the root objects in which only the fields that I mentioned come.

Comments

dbrgn

This is probably a duplicate of #15 (or at least closely related). Possibly [drf-flex-fields](https://github.com/rsinger86/drf-flex-fields) solves your use case? It's nontrivial to solve this for the general case and since I don't have much time to maintain this library I want to intentionally keep it simple (unless @jtrain thinks this is worth pursueing or unless a PR is submitted with a *very simple* implementation and tests). So currently recursive filtering is out of scope.

jtrain

I agree with @dbrgn here. Applying the filtering to child serializers could spiral our complexity. One way I'd solve this is to use a separate serializer for the children that doesn't have the fields you don't want. You needn't even use a ModelSerializer base, just a plain `serializer.Serializer` for a nice performance boost too.