Давайте предположим, что у нас есть эти модели, оригинальный проект отличается, но это было бы обычной задачей:
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, вкусный пирог - недружелюбный разработчик.