gpt4 book ai didi

django - 如何将 order_by 添加到 django rest 框架中的自定义序列化程序字段

转载 作者:行者123 更新时间:2023-12-04 23:04:45 25 4
gpt4 key购买 nike

序列化器:

class RequestSerializer(serializers.ModelSerializer):

travel_date = serializers.SerializerMethodField(method_name='get_traveldate')
guests = serializers.SerializerMethodField(method_name='get_num_of_guests')
cost = serializers.SerializerMethodField(method_name='get_invoice_cost')
status = serializers.SerializerMethodField(method_name='get_request_status')

class Meta:
model = Request
fields = (guests, cost,)


class BookingView(generics.ListApiView):

serializer_class = RequestSerializer
model = serializer_class.Meta.model
paginate_by = 15
filter_fields = ('costs', 'guests')

def get_queryset(self):
req_dict = self.request.query_params
client = int(req_dict.get('client'))
start_date = date(int(req_dict.get('from_year')), int(req_dict.get('from_month')), 1)
end_date = get_end_date(int(req_dict.get('to_year')), int(req_dict.get('to_month')))
requests = Request.objects.filter(guests__company__id=client, check_in__gte=start_date,
check_out__lte=end_date)

return requests

最佳答案

您可以将 MethodField 的逻辑移至 Request 类的ma​​nager

一旦您在管理器中拥有它,它们将可用于每个对象(序列化器、 View ...)并且您将能够使用它进行排序。

This is link will be helpful to it

关于django - 如何将 order_by 添加到 django rest 框架中的自定义序列化程序字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com