将 Django 查询对象 values() 传递到 Django Rest Framework Serializer

Passing Django query objects values() into Django Rest Framework Serializer

我试图获得与我的 Django 查询相同的输出,但当 Django Rest Framework 提供它时实际输出不同。将以下内容添加到我的序列化程序中,但它一直在输出中遗漏 cart_assessments__risk_type 。如何使我的 django rest 框架序列化器输出匹配我的 django 查询输出?

models.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)

views.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

serializers.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}

当点击 SubwayTrainDetailsViewSet 的 endpoint/route 时,Django Rest Framework 缺少 cart_assessments__risk_type 的实际输出:

# 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

谢谢@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')