如何通过tasytpie API将产品放入购物车? [英] How to put Product to Cart via tasytpie API?

查看:126
本文介绍了如何通过tasytpie API将产品放入购物车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我们有这些模型,原始项目有所不同,但这将是常见的任务:

  class Cart(models。模型):
owner = models.ForeignKey(User)
products = models.ManyToManyField(Product,symmetry = 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(.....):
#一个get是没有用的或是吗?
#一个帖子可以把一个产品放入购物车
#一个put(首选)可以把一个产品放在购物车
#删除可以从购物车中删除一个产品

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,自己).obj_create(bundle,request,owner = request.user)

def apply_authorization_limits(self,request,object_list):
如果len(object_list.filter(owner = request.user) )== 0:
Cart.objects.create(owner = request.user)
return object_list.filter(owner = request.user)

但我不知道该怎么办与django相比,tastypie是绝对的开发者 - 不友好。

解决方案

我认为你应该创建一个关系资源。请检查以下代码:

 类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',
}

然后,您可以向该资源发出POST请求以添加新产品到购物车。


Let's assume we have these models, original project differs but this would be the common task:

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

Now I want to put a Product into the Cart via the api.

I started like this:

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)

But I'm not sure what to do. Compared to django, tastypie is absolute developer-unfriendly.

解决方案

I think you should create a relationship resource. Pls check the code below:

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',
        }

Then you can make POST request to that resource to add a new Product to the Cart.

这篇关于如何通过tasytpie API将产品放入购物车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆