Jay P.
Jay P.

Reputation: 2590

How to work for nested objects in Django REST Framework

I'm new to Django REST Framework. In my Django models, I have Store and SimilarStore models. Store has similarstore_set which has multiple similar stores, and each similar store has store column as a foreign key showing its id. However, I wanna show the store object's columns instead of just id.

Like you see the below API example, the store with id 7 has 3 similar stores, and as the similar store has store foreign key, it shows the matched store's id number. I wanna show its child columns instead of just id number.

[
    {
        "id": 7,
        "status": "active",
        "businessName": "Ruby Claire Boutique",
            "similarstore_set": [
                {
                    "id": 1182,
                    "store": 7,     <--- wanna show the store's child columns
                },
                {
                    "id": 1208,
                    "store": 7,     <--- wanna show the store's child columns
                },
                {
                    "id": 1249,
                    "store": 7,     <--- wanna show the store's child columns
                }
            ]

    },
    ...
]

I'm trying to do that because when I show the detail page of store with id 7, I need to show the similar store's information in the store's detail page. However, I can't show enough information with just id.

I'm not sure if I'm approaching the way in the right direction. Can anyone help me about this?

EDIT

serializers.py

class SimilarStoreSerializer(ModelSerializer):

    store = StringRelatedField()

    class Meta:
        model = SimilarStore
        fields = ('id', 'store', )


class StoreSerializer(ModelSerializer):

    similarstore_set = SimilarStoreSerializer(many=True)

    class Meta:
        model = Store
        fields = ('id', 'status', 'businessName', 'similarstore_set', )

models.py

class Store(models.Model):

    status = CharField(...)
    businessName = CharField(...)
    ...


class SimilarStore(models.Model):

    store = ForeignKey(Store)
    ...

Upvotes: 0

Views: 516

Answers (1)

JPG
JPG

Reputation: 88459

Since your question incomplete, I assume similarstore_set is a reverse relationship data, and it's displayed with the help of nested serializer class.

Here you can try two solutions,

1. Define another serializer for Store model and link it in particular serializer. See this official doc for more info

2. Use depth attribute in Similarstore serializer's Meta class. See the doc


Example
Using depth,

class SimilarStoreSerializer(serializers.ModelSerializer):
    class Meta:
        model = SimilarStore
        fields = '__all__'
        depth = 1


class TopLevelSerializer(serializers.ModelSerializer):
    similarstore_set = SimilarStoreSerializer(many=True)

    class Meta:
        model = someModel
        fields = '__all__'

UPDATE-1

Add depth field in Meta class of SimilarStoreSerializer as

class SimilarStoreSerializer(ModelSerializer):
    # removed "store = StringRelatedField()"
    class Meta:
        model = SimilarStore
        fields = ('id', 'store', )
        depth = 1

UPDATE-2
Create a StoreMinimalSerializer, and define required fields according to your needs

class StoreMinimalSerializer(serializers.ModelSerializer):
    class Meta:
        model = Store
        fields = (add fields you want,)


class SimilarStoreSerializer(ModelSerializer):
    store = StoreMinimalSerializer()

    class Meta:
        model = SimilarStore
        fields = ('id', 'store',)

Upvotes: 2

Related Questions