我正在修改结帐视图,它接受单个产品/价格/数量、客户信息(名称、地址、电子邮件)和运费。在
问:我可以使用现有的django-oscarapi方法吗?换句话说,我希望实施一个只包含单个产品的篮子模型,并且在进行更新时使用(后进先出)后进先出。在
这是我的第一次尝试:
# This method is a hybrid of two oscarapi methods
# 1. oscarapi/basket/add-product
# 2. oscarapi/checkout
# It only allows one product to be added to the
# basket and immediately freezes it so that the
# customer can checkout
def post(self, request, format=None):
# deserialize the product from json
p_ser = self.add_product_serializer_class(
data=request.data['addproduct'], context={'request': request})
if p_ser.is_valid():
# create a basket
basket = operations.get_basket(request)
# load the validate the product
product = p_ser.validated_data['url']
# load the validated quantity
quantity = p_ser.validated_data['quantity']
# load any options
options = p_ser.validated_data.get('options', [])
#validate the basket
basket_valid, message = self.validate(
basket, product, quantity, options)
if not basket_valid:
return Response(
{'reason': message},
status=status.HTTP_406_NOT_ACCEPTABLE)
# add the product to the validated basket
basket.add_product(product, quantity=quantity, options=options)
# apply offers
operations.apply_offers(request, basket)
###### from oscarapi.views.checkout
# deserialize the checkout object and complete the order
co_data = request.data['checkout']
co_data['basket'] = "http://127.0.0.1:8000/oscar-api/baskets/"+str(basket.pk)+"/"
c_ser = self.checkout_serializer(
data=co_data, context={'request': request})
if c_ser.is_valid():
order = c_ser.save()
basket.freeze()
o_ser = self.order_serializer_class(
order, context={'request': request})
oscarapi_post_checkout.send(
sender=self, order=order, user=request.user,
request=request, response=response)
return response.Response(o_ser.data)
return response.Response(c_ser.errors, status.HTTP_406_NOT_ACCEPTABLE)
原始答案是肯定的。在
现在…
正如您肯定已经注意到的,使用
oscar-api
实现这一点并不是一项简单的任务。oscar-api
它只是没有在这些特性中实现思维。它是一个restapi,没有别的。在最简单的方法…
我建议您更深入地研究
basket
应用程序并覆盖Basket.add_product
方法,以确保您想要的,简单。在看这个:
非常简单!在
相关问题 更多 >
编程相关推荐