django - how to return one to many

时间:2022-03-05 09:58:46

this is my model

这是我的模型

class plans(models.Model):
    plan_name = models.CharField(max_length=50)
    plan_price = models.IntegerField(default=0)
    plan_is_active = models.BooleanField(max_length=1, default='1')

    def __str__(self):
        return self.plan_name

class plan_cat(models.Model):
    cat_name = models.CharField(max_length=50)
    plan = models.ForeignKey(plans, on_delete=models.CASCADE)
    cat_is_active = models.BooleanField(max_length=1, default='1')

    def __str__(self):
        return self.cat_name

my serializer is

我的序列化器

class PlanSerializer(serializers.ModelSerializer):

    class Meta:
        model = plans
        fields = ('plan_name','plan_price')


class CatSerializer(serializers.ModelSerializer):

    plan = PlanSerializer()
    class Meta:
        model = plan_cat
        fields = ('cat_name','plan')

this is my view

这是我的观点

class plan_details(APIView):
    def get(self, requests):
        queryset = plan_cat.objects.filter(cat_is_active='1')
        serializer = CatSerializer(queryset, many=True)
        return Response(serializer.data)

when i call this view http://localhost:8000/plans/details/

当我调用这个视图http://localhost:8000/plans/details/时

getting this output

得到这个输出

[
    {
        "cat_name": "category 1",
        "plan": {
            "plan_name": "free",
            "plan_price": 0
        }
    },
    {
        "cat_name": "category 2",
        "plan": {
            "plan_name": "paid",
            "plan_price": 10
        }
    },
    {
        "cat_name": "category 3",
        "plan": {
            "plan_name": "free",
            "plan_price": 0
        }
    }
]

instead i want a output like this

而是像这样的输出

[
    {
        "plan_name": "free",
        "plan_price": 0,
        "cat_name": {
            "category 1",
            "category 3",
        }
    },
    {
        "plan_name": "paid",
        "plan_price": 0,
        "cat_name": {
            "category 2",
        }
    },
]

the problem is the foreign key is with the second model, so i cant serialize to get the grouped content output as mentioned above. do i have to rewrite the model or serializer class ?

问题是外键是用第二个模型的,所以我不能序列化来获得上面提到的分组内容输出。是否需要重写模型或序列化器类?

1 个解决方案

#1


1  

You can use reverse ralation name plan_cat_set. Rewrite your view and serializer to this:

您可以使用反向关系名称plan_cat_set。重写您的视图和序列化程序:

class plan_details(APIView):
    def get(self, requests):
        queryset = plans.objects.filter(cat_is_active='1')
        serializer = PlanSerializer(queryset, many=True)
        return Response(serializer.data)


class CatSerializer(serializers.ModelSerializer):

    class Meta:
        model = plan_cat
        fields = ('cat_name',)

class PlanSerializer(serializers.ModelSerializer):
    cat_name = CatSerializer(source='plan_cat_set', many=True)

    class Meta:
        model = plans
        fields = ('plan_name','plan_price', 'cat_name')

Note that source='plan_cat_set' on CatSerializer allow you to change serializer field name from default plan_cat_set to cat_name.

注意,在CatSerializer上的source='plan_cat_set'允许您将serializer字段名从默认的plan_cat_set更改为cat_name。

#1


1  

You can use reverse ralation name plan_cat_set. Rewrite your view and serializer to this:

您可以使用反向关系名称plan_cat_set。重写您的视图和序列化程序:

class plan_details(APIView):
    def get(self, requests):
        queryset = plans.objects.filter(cat_is_active='1')
        serializer = PlanSerializer(queryset, many=True)
        return Response(serializer.data)


class CatSerializer(serializers.ModelSerializer):

    class Meta:
        model = plan_cat
        fields = ('cat_name',)

class PlanSerializer(serializers.ModelSerializer):
    cat_name = CatSerializer(source='plan_cat_set', many=True)

    class Meta:
        model = plans
        fields = ('plan_name','plan_price', 'cat_name')

Note that source='plan_cat_set' on CatSerializer allow you to change serializer field name from default plan_cat_set to cat_name.

注意,在CatSerializer上的source='plan_cat_set'允许您将serializer字段名从默认的plan_cat_set更改为cat_name。