How to implement nested serializer in drf-flex-fields

I stumbled upon implementing a nested serializer, so I'll write it as a memo.

Install drf-flex-fields

pip install drf-flex-fields

model

model.py


class User(models.Model):
    username = models.CharField(max_length=255)


class Tag(models.Model):
    name = models.CharField(max_length=255)


class Book(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    tag = models.ManyToManyField(Tag)

serializer

serializers.py


from rest_flex_fields import FlexFieldsModelSerializer


class UserSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = ("id", "username")


class TagSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = ("id", "name")


class BookSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Book
        fields = ("id", "title", "author", "tag")
        expandable_fields = {                        #expandable_Nested in fields
            "author": UserSerializer,
            "tag": (TagSerializer, {"many": True})   #Many-to-many
        }

Response

[
    {
        "id": 1,
        "title": title_1,
        "author": 1,
        "tag": [
            1,
            2,
        ]
    },
]

Dynamic display of data

Display dynamic data with parameters

option Explanation
expand Expand the specified field
fields Show only specified fields
omit The specified field is not displayed

expand http://localhost:8000/book/1?expand=author,tag Expand author and tag

[
    {
        "id": 1,
        "title": title_1,
        "author": {
            "id": 1,
            "username": "user_1"
        },
        "tag": [
            {
                "id": 1,
                "name": "tag_1"
            },
                        {
                "id": 2,
                "name": "tag_2"
            },
        ]
    },
]

fields http://localhost:8000/book/1?fields=title Display only the title

[
    {
        "title": title_1,
    },
]

omit http://localhost:8000/book/1?omit=title Do not display the title

[
    {
        "id": 1,
        "author": 1,
        "tag": [
            1,
            2,
        ]
    },
]

reference

rsinger86/drf-flex-fields - github drf-flex-fields - pypi

Recommended Posts

How to implement nested serializer in drf-flex-fields
How to implement Scroll View in pythonista 1
How to implement Rails helper-like functionality in Django
How to implement Discord Slash Command in Python
How to implement shared memory in Python (mmap.mmap)
How to implement a gradient picker in Houdini
How to develop in Python
How to temporarily implement a progress bar in a scripting language
How to implement Python EXE for Windows in Docker container
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to write soberly in pandas
How to use SQLite in Python
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
How to implement Java code in the background of RedHat (LinuxONE)
How to use calculated columns in CASTable
I tried to implement PLSA in Python
How to access environment variables in Python
I tried to implement permutation in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to convert csv to tsv in CLI
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
I tried to implement PLSA in Python 2
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe
How to check opencv version in python
I tried to implement ADALINE in Python
How to enable SSL (TLS) in Apache
How to use Anaconda interpreter in PyCharm
How to specify non-check target in Flake8
I tried to implement PPO in Python
How to handle consecutive values in MySQL
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to do Server-Sent Events in Django
How to use regular expressions in Python
How to convert DateTimeField format in Django
How to use Map in Android ViewPager
How to display Hello world in python
How to read CSV files in Pandas
How to change editor color in PyCharm
How to write this process in Perl?