django-tastypie : Posting to a Resource having ManytoMany field with through relationship

标签 django tastypie

我正在为一个项目开发 API,我通过 OrderProducts 建立了 Order/Products 关系,如下所示:

在models.py中

class Product(models.Model):
    ...

class Order(models.Model):
    products = models.ManyToManyField(Product, verbose_name='Products', through='OrderProducts')
    ...

class OrderProducts(models.Model):
    order = models.ForeignKey(Order)
    product = models.ForeignKey(Product)
    ...

现在,当我通过 API 加载订单时,我也想获取相关产品,所以我尝试了这个(使用 django-tastypie):

顺序/api.py
class OrderResource(ModelResource):
    products = fields.ToManyField('order.api.ProductResource', products, full=True)

    class Meta:
        queryset = Order.objects.all()
        resource_name = 'order'

一切都适用于列出订单资源。我获得了嵌入产品数据的订单资源。

问题是我无法使用 api 创建或编辑 Order 对象。由于我在多对多关系中使用直通模型,因此 ManyToManyField(products) 没有 .add() 方法。但是当向 OrderResource 发布/放入数据时,tastypie 试图在 OrderResource 的 products 字段上调用 ​​.add() 。
{"error_message": "'ManyRelatedManager' object has no attribute 'add'", "traceback": "Traceback (most recent call last):\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 192, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 397, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 427, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1165, in post_list\n    updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs))\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1784, in obj_create\n    self.save_m2m(m2m_bundle)\n\n  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1954, in save_m2m\n    related_mngr.add(*related_objs)\n\nAttributeError: 'ManyRelatedManager' object has no attribute 'add'\n"}

最佳答案

由于您需要 manytomany 字段 仅供上市 ,更好的解决方案是添加 readonly=TrueOrderResourceproducts field 。这消除了覆盖 save_m2m 的需要方法。为了完整性:

class OrderResource(ModelResource):
    products = fields.ToManyField('order.api.ProductResource', products, 
                                  readonly=True, full=True)

    class Meta:
        queryset = Order.objects.all()
        resource_name = 'order'

关于django-tastypie : Posting to a Resource having ManytoMany field with through relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767740/

相关文章:

python - 如何在 django tastypie json 上附加 success=true

python - Django 管理员 : Using a custom widget for only one model field

sql - Django sqlall <app> 没有返回任何东西

python - Tastypie 属性和相关名称,空属性错误

python - Django-Tastypie 中的多部分/表单数据 POST、PUT、PATCH

Django Tastypie : Getting extra-values of a m2m relationships using intermediate model

python - 更改 Django 管理员 list_display 中的可点击字段

python - 在 Windows Azure 上配置 Python 3.4 和 Django

python - Django - 通过 OneToOneField 访问属性

python - Django Tastypie 使用 PATCH 或 PUT 请求进行多对多( self )字段更新?