Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails for recursive fields. #25

Closed
noodlebreak opened this issue Jul 18, 2018 · 2 comments
Closed

Fails for recursive fields. #25

noodlebreak opened this issue Jul 18, 2018 · 2 comments

Comments

@noodlebreak
Copy link

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.

@dbrgn
Copy link
Owner

dbrgn commented Jul 18, 2018

This is probably a duplicate of #15 (or at least closely related). Possibly 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.

@dbrgn dbrgn closed this as completed Jul 18, 2018
@jtrain
Copy link
Collaborator

jtrain commented Jul 18, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants