Models.py
class Order(models.Model):
order_name = models.CharField(max_length=10,unique = True, default = "")
class LineItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE, null=True, related_name="items")
product = models.ForeignKey(Product, on_delete=models.CASCADE,null=True, related_name="prod")
quantity = models.IntegerField(default = 0)
class Product(models.Model):
product_name = models.CharField(max_length=10)
product_price = models.DecimalField(max_digits=10,decimal_places=2)
因此,我试图创建一个APIView PUT请求以能够更改订单内容。因此,我在OrderItemSerializer中的create方法无法正常工作。我是Django的新手。def update(self,instance,validated_data):
因此,基本上我无法完成此功能。
这是我的实例的结构-
{
"order_name": "OrderA",
"line_items": [
{
"product": {
"id": 7,
"product_name": "ProdA",
"product_price": "23.99"
},
"quantity": 2,
"product_total": 47.98
},
{
"product": {
"id": 9,
"product_name": "ProdC",
"product_price": "19.99"
},
"quantity": 23,
"product_total": 459.77
}
],
"cart_total": 507.75
}
Serializers.py
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['product_name','product_price']
class OrderSerializer(serializers.ModelSerializer):
line_items = LineItemSerializer(many=True, source='items')
class Meta:
model = Order
fields = ['order_name', 'line_items', 'cart_total']
def create(self, request):
serialized = self.serializer_class(data=request.DATA)
if serialized.is_valid():
serialized.save()
return Response(status=HTTP_202_ACCEPTED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class LineItemSerializer(serializers.ModelSerializer):
class Meta:
model = LineItem
depth =1
fields = ('product', 'quantity', 'product_total')
Views.py
class LineItemList(APIView):
def get(self,request):
lineitemlist = LineItem.objects.all()
serializer = LineItemSerializer(lineitemlist,many = True)
return Response(serializer.data)
def put(self, request):
snippet = LineItem.objects.all()
serializer = LineItemSerializer(snippet, data=request.data, partial = True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, order_name):
snippet = self.get_object(order_name)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
class OrderList(APIView):
def get_object(self, order_name):
try:
return Order.objects.get(order_name=order_name)
except Order.DoesNotExist:
raise Http404
def get(self,request):
orderlist = Order.objects.all()
serializer = OrderSerializer(orderlist,many = True)
return Response(serializer.data)
def post(self, request):
serializer = OrderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
order_name = body['order_name']
snippet = Order.objects.get(order_name=order_name)
serializer = OrderSerializer(snippet, data=request.data, partial = True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
order_name = body['order_name']
snippet = self.get_object(order_name)
snippet.delete()
class OrderDetail(APIView):
def get_object(self, order_name):
try:
return Order.objects.get(order_name=order_name)
except Order.DoesNotExist:
raise Http404
def get(self, request, order_name):
snippet = Order.objects.get(order_name=order_name)
serializer = OrderSerializer(snippet)
return Response(serializer.data)
问题是,如果我使用RelatedManager(实例中的引用line_items,则无法再进行任何级别操作)。我只能选择在产品,数量和编号之间进行选择。 我正在努力的更新方法
def update(self, instance, validated_data):
listitems = instance.items.get(product_name = validated_data.get("product_name"))
items = instance.items.return_set(validated_data.get("product_name"))
items = instance.objects.items.all().select_related('product').select_related('product_name')
items = items.product_name
instance.save()
a = items.get('product_name'= validated_data.get("product_name")
asper= key.get(product_name=validated_data.get("product_name"))
items.product.get(produc_name=validated_data.get("product_name")
for prod in items.product.all():
if prod.product_name==validated_data.get("product_name"):
prod.quantity = validated_data.get("quantity")
prod.save()
return instance