drf-flex-fields icon indicating copy to clipboard operation
drf-flex-fields copied to clipboard

Enhancement: magic field expansion

Open danihodovic opened this issue 9 months ago • 0 comments

This is a great library and a recent discovery of mine. I found declaring expandable_fields tedious and wanted to automatically discover expandable_fields based on model relationships. I came up with the snippet below in one of my projects. I was wondering if you're open to a pull-request where we add the functionality?

We could use the __all__ convention, common in DRF, to make all related fields expandable.

from myapp.api.serializers.utils import MagicFlexFieldsModelSerializer

class UserSerializer(MagicFlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = [
            "id",
            "name",
        ]
        expandable_fields = "__all__"

https://django.wtf/blog/graphql-like-features-in-django-rest-framework/#magically-discovering-expandable_fields

danihodovic avatar May 23 '24 19:05 danihodovic