3
假设我们有这些车型,原来的项目不同,但是这将是共同的任务:如何通过tasytpie API将产品放入购物车?
class Cart(models.Model):
owner = models.ForeignKey(User)
products = models.ManyToManyField(Product, symmetrical=False)
class Product(models.Model):
title = models.CharField(max_length="255")
description = models.TextField()
现在我想提出一个产品到通过API的车。
我开始是这样的:
class CartResource(ModelResource):
products = fields.ManyToManyField(ProductResource, 'products', full=True)
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/product/(?P<prodcut_id>\w[\w/-]*)/$" % (self._meta.resource_name), self.wrap_view('dispatch_detail_product'), name="api_dispatch_detail_product"),
]
def dispatch_detail_product(.....):
# A get is not useful or is it?
# A post could put a product into the cart
# A put (preferred) could put a product in the cart
# A delete could delete a product from the cart
class Meta:
queryset = Product.objects.all()
authentication = MyBasicAuthentication()
authorization = DjangoAuthorization()
list_allowed_methods = ['get']
detail_allowed_methods = ['get', 'put', 'delete']
def obj_update(self, bundle, request=None, **kwargs):
return super(PrivateSpaceResource, self).obj_create(bundle, request, owner=request.user)
def apply_authorization_limits(self, request, object_list):
if len(object_list.filter(owner=request.user)) == 0:
Cart.objects.create(owner=request.user)
return object_list.filter(owner=request.user)
但我不知道该怎么做。与django相比,tastypie绝对是开发者不友好的。
我标记这个问题已解决,但我没有测试它!没有使用tastypie。 – seb 2013-05-10 22:32:21