class DiscountControllAPiView(APIView):
"""
each product may takes a discount code or percent from owner or site administrator
"""
def post(self,request):
serializer = ProductDiscountControllSerializer(data=request.data)
if(serializer.is_valid()):
serializer.save()
return Response(True)
else:
return Response(serializer.errors)
相关的血清化剂:
class ProductDiscountControllSerializer(ModelSerializer):
class Meta:
model = ProductDiscountControll
fields = [
'product',
'discount',
'discount_code',
'discount_code_precent',
]
答案 0 :(得分:0)
试试这个
class DiscountControllAPiView(APIView):
"""
each product may takes a discount code or percent from owner or site administrator
"""
def post(self,request):
id = request.data.get('id', None):
if not id:
# perform creation
serializer = ProductDiscountControllSerializer(data=request.data)
else:
# perform updation
product_discount_controll = ProductDiscountControll.objects.get(id=int(id))
serializer = ProductDiscountControllSerializer(product_discount_controll, data=request.data)
if(serializer.is_valid()):
serializer.save()
return Response(True)
else:
return Response(serializer.errors)
如果请求中存在id
,则api将更新,否则将创建新记录