示例#1
0
    def list(self, request):
        queryset = self.filter_queryset(request)
        page = self.paginate_queryset(queryset)

        if page is not None:
            serializer = CustomerSerializer(page,
                                            many=True,
                                            context={'request': request})
            response = self.get_paginated_response(serializer.data)
            response['draw'] = int(request.GET.get('draw', 1))
            response['recordsTotal'] = len(self.base_queryset())
            return Response(response)

        serializer = CustomerSerializer(queryset,
                                        many=True,
                                        context={'request': request})
        return Response(serializer.data)
示例#2
0
class  OrderSerializer(serializers.ModelSerializer):
    customer = CustomerSerializer()
    order_items = OrderItemSerializer(source='orderitem_set',many=True)

    class Meta:
        model = Order
        fields = ['id','customer','status','total_price','order_items']
        read_only_fields = ['total_price']

    def validate_status(self, value):
        if value == 'delivered':
            raise serializers.ValidationError("Changing to this status is prohibited")
        return value
示例#3
0
 def create(self, request):
     new_customer = CustomerSerializer(data=request.data)
     if new_customer.is_valid():
         new_customer.save()
         return Response(new_customer.data, status=201)
     return Response(new_customer.errors, status=400)
示例#4
0
 def retrieve(self, request, pk=None):
     queryset = Customer.objects.all()
     user = get_object_or_404(queryset, pk=pk)
     serializer = CustomerSerializer(user)
     return Response(serializer.data)
示例#5
0
 def list(self, request):
     queryset = Customer.objects.all()
     serializer = CustomerSerializer(queryset, many=True)
     return Response(serializer.data)