如何通过 tasytpie API 将产品放入购物车?

2023-12-15

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

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 绝对对开发人员不友好。


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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 tasytpie API 将产品放入购物车? 的相关文章

随机推荐

  • 使用最简单的方法将数据(字符串)从子 VC 传递到父 VC

    使用最简单的方法将数据 字符串 从子视图控制器传递到父 VC 我尝试了几种方法 但迷失了 有人可以告诉我最好的方法吗 斯里坎特是正确的 如果您有从一个视图控制器 我们的 第一个 视图控制器 到另一个视图控制器 我们的 第二个 视图控制器 的
  • 单击坐标而不识别元素

    作为我的 Selenium 登录功能测试的一部分 我想通过识别按钮的坐标并指示 Selenium 单击这些坐标来单击按钮 这可以在不识别元素本身的情况下完成 通过 id xpath 等 我知道还有其他更有效的方法来运行单击命令 但我希望专门
  • 即使浏览器关闭,CodeIgniter 如何保留会话数据?

    我想知道 CodeIgniter 中的会话是如何工作的 当浏览器关闭时 会话不是应该自动终止吗 默认情况下 CodeIgniter 不会在浏览器关闭时销毁会话 config sess expire on close FALSE 相反 我们可
  • Chart.js 工具提示模板不起作用

    因此 我正在 Chart js 中使用条形图 并且尝试让自定义工具提示正常工作 环顾四周 似乎在这种情况下要做的事情是添加 tooltipTemplate test 到我的选项部分 这将在结果工具提示中的数据值后面显示单词 test 然而
  • 在 SQL 中存储用户定义数据的正确方法

    我想构建一个类似于 wufoo 的在线表单生成器 允许用户创建和发布自己的 Web 表单 每个提交内容都应保存到数据库中 用户稍后可以在数据库中检索提交内容 因为这些形式将是动态的 即 用户可以完全控制表单字段的数量和类型我正在尝试考虑一个
  • PHP - 当变量放入 url 时,include() 文件不起作用?

    在 PHP 中 我构建了一个网页 它使用 include 来加载网站的某些部分 然而 我现在遇到了一个类似的问题 当我使用如下网址时 data openov storingen php type actueel它给了我这个错误 Warnin
  • 如何通过 tasytpie API 将产品放入购物车?

    假设我们有这些模型 原始项目有所不同 但这将是常见任务 class Cart models Model owner models ForeignKey User products models ManyToManyField Product