5

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

在模型.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 中的 products 字段发布/放置数据时,tastepie 会尝试调用 .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"}
4

3 回答 3

5

由于您只需要 manytomany 字段来列出,因此更好的解决方案是添加readonly=Trueon字段。这消除了覆盖方法的需要。为了完整性:OrderResourceproductssave_m2m

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

    class Meta:
        queryset = Order.objects.all()
        resource_name = 'order'
于 2013-07-14T20:10:16.767 回答
3

解决方案在于覆盖资源上的 save_m2m() 方法。在我的情况下,我需要 manytomany 字段仅用于列出,因此重写了 save_m2m() 方法以不做任何事情。

于 2012-12-13T09:04:49.947 回答
1

如果您被允许修改class OrderProducts,添加auto_created = True可能会解决您的问题,即

class OrderProducts(models.Model): 
    class Meta:
        auto_created = True

如果您无法更改class OrderProducts,请尝试以下美味派补丁。

---------------------------- tastypie/resources.py ----------------------------
index 2cd869e..aadf874 100644
@@ -2383,7 +2383,20 @@ class BaseModelResource(Resource):
                     related_resource.save(updated_related_bundle)
                 related_objs.append(updated_related_bundle.obj)

-            related_mngr.add(*related_objs)
+            if hasattr(related_mngr, 'through'):
+                through = getattr(related_mngr, 'through')
+                if not through._meta.auto_created:
+                    for related_obj in related_objs:
+                        args = dict()
+                        args[related_mngr.source_field_name] = bundle.obj
+                        args[related_mngr.target_field_name] = related_obj
+                        through_obj = through(**args)
+                        through_obj.save()
+                else:
+                    related_mngr.add(*related_objs)
+            else:
+                related_mngr.add(*related_objs)

     def detail_uri_kwargs(self, bundle_or_obj):
         """

在 Django 1.7 中,错误消息更改为“无法在指定中间模型的 ManyToManyField 上设置值”。解决方法是一样的。

于 2015-01-20T06:22:02.993 回答