object_254

Фильтр запросов во вложенном сериализаторе

Dec 1st, 2021
709
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BookingFilteredListSerializer(serializers.ListSerializer): # класс List Serializer
  2.     def to_representation(self, data):
  3.         data = data.filter(status__in=['open', 'close'])  # фильтр
  4.         return super(BookingFilteredListSerializer, self).to_representation(data)
  5.  
  6.  
  7. class BookingHistorySerializer(serializers.ModelSerializer):
  8.     table = TableForHistorySerializer()
  9.  
  10.     class Meta:
  11.         model = Booking
  12.         list_serializer_class = BookingFilteredListSerializer # вызов сериализатора фильтра
  13.         fields = ('id', 'time_start', 'table', 'status',)
  14.  
  15.  
  16. class GuestFullSerializer(serializers.ModelSerializer):
  17.     booking = BookingHistorySerializer(many=True)
  18.     bookings = BookingHistorySerializer(many=True)
  19.  
  20.     class Meta:
  21.         model = Guest
  22.         fields = '__all__'
RAW Paste Data