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)
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
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)
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)
def list(self, request): queryset = Customer.objects.all() serializer = CustomerSerializer(queryset, many=True) return Response(serializer.data)