2

我试图获得与我的 django 查询相同的输出,但是当 Django Rest Framework 提供服务时,实际输出会有所不同。在我的序列化程序中添加了以下内容,但它一直忽略cart_assessments__risk_type输出。如何使我的 django rest 框架序列化程序输出与我的 django 查询输出匹配?

模型.py:

from django.db import models

class TrainAssessment(models.Model):
    train_name = models.CharField(max_length=30)


class CartAssessment(models.Model):
    train_assessment = models.ForeignKey(TrainAssessment, on_delete=models.CASCADE, related_name='cart_assessments')
    risk_type = models.CharField(max_length=30)

视图.py

from rest_framework import viewsets, mixins

class SubwayTrainDetailsViewSet(viewsets.GenericViewSet,
                            mixins.ListModelMixin,
                            mixins.CreateModelMixin):
    queryset = TrainAssessment.objects.values('cart_assessments__risk_type').annotate(
        cart_count=Count('cart_assessments__risk_type')).order_by('-cart_count').annotate(
        train_count=Count('id', distinct=True))
    serializer_class = serializers.SubwayTrainDetailsViewSetSerializer

序列化程序.py

from rest_framework import serializers

class SubwayTrainDetailsViewSetSerializer(serializers.ModelSerializer):
    train_count = serializers.IntegerField()
    cart_count = serializers.IntegerField()
    cart_assessments__risk_type = serializers.RelatedField(source="cartassessment.risk_type", read_only=True)

    class Meta:
        model = TrainAssessment
        fields = ('id', 'cart_assessments__risk_type', 'train_count', 'cart_count')

我试图让我的序列化程序提供与以下相同的输出:

#QUERY
queryset = TrainAssessment.objects.values('cart_assessments__risk_type').annotate(
    cart_count=Count('cart_assessments__risk_type')).order_by('-cart_count').annotate(
    train_count=Count('id', distinct=True))

#OUTPUT I WANT THAT COMES FROM ABOVE QUERY:
{'cart_assessments__risk_type': '', 'cart_count': 55, 'train_count': 14}
{'cart_assessments__risk_type': 'door', 'cart_count': 22, 'train_count': 13}
{'cart_assessments__risk_type': 'wheel', 'cart_count': 8, 'train_count': 8}
{'cart_assessments__risk_type': 'frame', 'cart_count': 1, 'train_count': 1}
{'cart_assessments__risk_type': 'floors', 'cart_count': 1, 'train_count': 1}
{'cart_assessments__risk_type': 'windows', 'cart_count': 1, 'train_count': 1}
{'cart_assessments__risk_type': 'straphanger', 'cart_count': 1, 'train_count': 1}

cart_assessments__risk_type到达 SubwayTrainDetailsViewSet 的端点/路线时,Django Rest Framework缺少实际输出:

# OUTPUT MISSING `cart_assessments__risk_type`
[
    {"train_count": 14, "cart_count": 55},
    {"train_count": 13, "cart_count": 22},
    {"train_count": 8, "cart_count": 8},
    {"train_count": 1, "cart_count": 1},
    {"train_count": 1, "cart_count": 1},
    {"train_count": 1, "cart_count": 1},
    {"train_count": 1,"cart_count": 1}
]

我如何 django rest 框架使用cart_assessments__risk_type?p提供输出

4

1 回答 1

1

谢谢@tarasinf。我能够通过您提到的解决方案提出答案。

class SubwayTrainDetailsViewSetSerializer(serializers.ModelSerializer):
    train_count = serializers.IntegerField()
    cart_count = serializers.IntegerField()
    cart_assessments__risk_type = serializers.SerializerMethodField('get_cart_assessments')

    class Meta:
        model = TrainAssessment
        fields = ('cart_assessments__risk_type', 'train_count', 'cart_count')

    def get_cart_assessments(self, obj):
        return obj.get('cart_assessments__risk_type')
于 2020-03-04T15:44:21.853 回答