0

目前我正在我的项目中实现 django-rest-framework。
我想要来自我的序列化程序(这是一个序列化关系)的自定义响应,但同时我也想使用带有查询参数的自定义查询集,所以我get_queryset()在我的generics.ListAPIView. 使用这个我无法在我的回复中获得我的自定义序列化关系。


序列化程序.py

class ChainedSerializer(serializers.ModelSerializer):
    cate = serializers.SerializerMethodField()
    sub_cate = serializers.SerializerMethodField()

    class Meta:
        model = Chained
        exclude = ('id', 'shop')

    def get_cate(self, obj):
        cat = Cate.objects.get(id=obj.cate.id)
        print(cat)
        return cat.cate_name

    def get_sub_cate(self, obj):
        sub_cats = SubCate.objects.get(id=obj.sub_cate_id).sub_cate_name
        return sub_cats


class ShopSerializer(serializers.ModelSerializer):
    shop = ChainedSerializer(read_only=True, many=True)

    class Meta:
        model = Shop
        fields = '__all__'


视图.py

class ShopList(generics.ListAPIView):
    queryset = Shop.objects.all()
    serializer_class = ShopSerializer
    permission_classes = (permissions.AllowAny,)

    def get_queryset(self):
        subcate = self.request.query_params.getlist('subcate')

        subcate_ids = list(
            SubCate.objects.filter(sub_cate_name__in=subcate).values_list('id', flat=True))

        shop_ids = list(Chained.objects.filter(sub_cate_id__in=subcate_ids).values_list(
            'shop_id', flat=True))

        queryset = Shop.objects.filter(id__in=shop_ids).values()

        return queryset

使用 ```get_queryset()``` 方法时的响应
[ {
        "id": 1,
        "shop_type": "willodale",
        "shop_name": "first shop",
        "shop_email": "something@gmail.com",
        "phone": "1111111111",
        "area_code": "11111",
        "area_name": "dummy",
        "shop_address": "dummy",
    }
]

如果我不使用该get_queryset()方法,请响应:

[
    {
        "id": 2,
        "shop": [
            {
                "cate": "Serv",
                "sub_cate": "WI"
            }
        ],
        "shop_type": "type",
        "shop_name": "dummy2",
        "shop_email": "something2@gmail.com",
        "phone": "1111111111",
        "area_code": "11111",
        "area_name": "dummy",
        "shop_address": "dummy",
    },
    {
        "id": 1,
        "shop": [
            {
                "cate": "Serv",
                "sub_cate": "Park"
            },
            {
                "cate": "Amb",
                "sub_cate": "Rom"
            },
            {
                "cate": "Serv",
                "sub_cate": "WI"
            }
        ],
        "shop_type": "type2",
        "shop_name": "dummy",
        "shop_email": "something@gmail.com",
        "phone": "1111111111",
        "area_code": "11111",
        "area_name": "dummy",
        "shop_address": "dummy",
    }
]

还附上了我的models.py

class Shop(models.Model):
    shop_type = models.CharField(choices=SHOP_TYPE, default='type', max_length=255)
    shop_name = models.CharField(max_length=255)
    shop_email = models.EmailField()
    phone = models.CharField(max_length=10)
    area_code = models.CharField(max_length=255)
    area_name = models.CharField(max_length=255)
    shop_address = models.TextField()

    def __str__(self):
        return self.shop_name


class Cate(models.Model):
    cate_name = models.CharField(max_length=255)

    def __str__(self):
        return self.cate_name


class SubCate(models.Model):
    cate = models.ForeignKey(Cate, on_delete=models.CASCADE)
    sub_cate_name = models.CharField(max_length=255)

    def __str__(self):
        return self.sub_cate_name


class Chained(models.Model):
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE, related_name='shop')
    cate = models.ForeignKey(Cate, on_delete=models.CASCADE, related_name='cate_chained')
    sub_cate = ChainedForeignKey(
        SubCate,
        chained_field="cate",
        chained_model_field="cate",
        sort=True)

我可以将get_queryset过滤器与我的自定义序列化响应一起使用,还是有任何其他方法可以实现这一点,以便我可以获得我的序列化响应和过滤器

4

0 回答 0