¿Cómo poner el producto en el carrito a través de API tasytpie?

Supongamos que tenemos estos modelos, el proyecto original difiere, pero esta sería la tarea común:

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() 

Ahora quiero poner un producto en el carrito a través de la api.

Comencé así:

 class CartResource(ModelResource): products = fields.ManyToManyField(ProductResource, 'products', full=True) def override_urls(self): return [ url(r"^(?P%s)/product/(?P\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) 

Pero no estoy seguro de qué hacer. Comparado con django, tastypie es absolutamente hostil para desarrolladores.

Creo que deberías crear un recurso de relación. Por favor revise el siguiente código:

 class LikeResource(ModelResource): profile = fields.ToOneField(ProfileResource, 'profile',full=True) post = fields.ToOneField(PostResource,'post') class Meta: queryset = Like.objects.all() authentication = ApiKeyAuthentication() authorization = DjangoAuthorization() resource_name = 'like' filtering = { 'post': 'exact', 'profile':'exact', } 

Luego, puede realizar una solicitud POST a ese recurso para agregar un nuevo producto al carrito.