【问题标题】:how to format field name and round the value如何格式化字段名称并舍入值
【发布时间】:2022-11-02 21:21:54
【问题描述】:

根据以下相关表格,我得到了一个查询“总发票价值的前 3 名员工”:

    
    [{
        "customerid__supportrepid__id": 3,
        "sells": 833.0400000000013
    },
    ...]

我希望第一个文件是:“employee_id”和一个销售字段值

    class CustomerViewSet(viewsets.ReadOnlyModelViewSet):

    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

    @action(detail=False, methods=['get'])
    def top_three_employees(self, request):
        total_sells_by_employees = Invoice.objects \
                                                .select_related('customerid') \
                                                .select_related('customerid__supportrepid') \
                                                .values('customerid__supportrepid__id') \
                                                .annotate(sells=Sum('total')) \
                                                .order_by('-sells')

        return Response(total_sells_by_employees)

【问题讨论】:

    标签: django-rest-framework


    【解决方案1】:
    You can use .annotate() to create an aliased, transformed field, and then serialize that field:
    from django.db.models.functions import Round
    
    MyModel.objects.annotate(
        rounded_field=Round('field_name', 2)
    ).values('rounded_field')
    This will add a rounded_field to the queryset that is the field_name rounded to two digits.
    You can then rename this in the serializer fields mapping, with:
    class MyModelSerializer(serializers.ModelSerializer):
        rounded_field = serializers.SerializerMethodField()
    
        class Meta:
            fields = ('rounded_field',)
        
        def get_rounded_field(self, instance):
            return instance.rounded_field
    This will thus serialize the rounded_field as rounded_field, under the name you specified in the fields of the serializer.
    

    【讨论】:

      【解决方案2】:
      You can rename fields in a Django queryset using the values() method [django-doc]:
      MyModel.objects.values(renamed_field_name=F('original_field_name'))
      This will thus construct a queryset where the original_field_name is renamed to renamed_field_name.
      In a serializer, you can then rename the field with:
      class MySerializer(serializers.Serializer):
          renamed_field_name = serializers.CharField()
      So by defining a field with the target name, and omitting a source, it will automatically take the field with the same name in the object that is serialized.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 2020-01-27
        • 2014-05-28
        • 1970-01-01
        相关资源
        最近更新 更多