Nest objects related tables Django API REST Framework


I have 3 related tables, 1 - > 2 - > 3 Now with mysql we would do inner join of the tables and now we could filter by fields of the 3, the thing is that with the REST API I would like to nest all the elements of the 3 related to the 2 related to the once with the 1st and I just calling the first one.

The thing is that I get to nest all the tables related to the 1, but all that are related to those that are related to the 1 can not access them or nest me.

that is, I can do a 1Model.object.filter (2__2_name="Antonio") it works, but I would like to be able to 1Model.object.filter (2__2_name="Antonio", 3__3_type="House") and return all nested 1 to 2 and 3, just calling 1.

How should I relate the serializers or how to filter the views so I can access the third level?

I do not know if I explain myself very clearly but to give an example:

I would like to be able to filter with the REST API of Django that I return all the questions and answers of a brand and a specific language and I do not know how to relate the serializers or get that info in the view or viewset. The filtering of a single table or one that is directly related is no problem, but when I have to cross so much I get messed up.

How should I do it? Django Multiple Models, ModelViewset, ViewsetMixin or how? The documentation is not clear enough in my opinion.

asked by RuralGalaxy 26.05.2016 в 13:06

2 answers


I imagine that you want to show information of the 3 related tables just by calling the lowest hierarchy. In my case I have a User > Profile > Doctor and what I do is this:

class UserSerializer(serializers.ModelSerializer):
    full_name = serializers.SerializerMethodField(read_only=True)

    def get_full_name(self, obj):
        return '%s %s' % (obj.first_name, obj.last_name)

    class Meta:
        model = User
        fields = ('id', 'username', 'full_name', 'first_name', 'last_name', 'email')

class ProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer(many=False, read_only=True)

    class Meta:
        model = Profile
        fields = ('id', 'user', 'role', 'code', 'telephone', 'telephone2')

class DoctorSerializer(serializers.ModelSerializer):
    profile = ProfileSerializer(many=False, read_only=True)

    class Meta:
        model = Doctor
        fields = ('id', 'profile', 'code', 'specialities', 'status')

Now we only have to add DoctorSerializer to the view and filter it with the use of the Doctor table. With this you can already show the information of the 3 related tables, the result would be:

        "id": 3,
        "profile": {
            "id": 4,
            "user": {
                "id": 3,
                "username": "rmorales",
                "full_name": "Ricardo Morales",
                "first_name": "Ricardo",
                "last_name": "Morales",
                "email": "[email protected]",
                "profile": 4
            "role": 1,
            "code": "1234 44444 1234",
            "telephone": "3000",
            "telephone2": "4000"
        "code": "12123",
        "specialities": [],
        "status": 1

It only remains to replace your tables. You can see the info in the official documentation .

answered by 08.06.2016 в 16:10

A simple way would be to use depth in the serializer:

class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = ('id', 'name', 'languages', 'created')
        depth = 3
answered by 03.11.2016 в 08:20