django-tastypie:发布到具有ManytoMany字段且具有完全关系的资源

时间:2012-12-07 17:09:24

标签: 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对象。由于我在ManytoMany关系中使用直通模型,因此ManyToManyField(产品)没有.add()方法。但是tastypie试图在发布/向其发送数据时在OrderResource的产品字段上调用.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"}

3 个答案:

答案 0 :(得分:5)

由于您需要多个字段仅用于列表,因此更好的解决方案是在readonly=True的{​​{1}}字段上添加OrderResource。这消除了重写products方法的需要。为了完整性:

save_m2m

答案 1 :(得分:3)

解决方案在于覆盖资源上的save_m2m()方法。在我的情况下,我需要manytomany字段仅列出,所以重写save_m2m()方法什么都不做。

答案 2 :(得分:1)

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

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

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

---------------------------- 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上设置值,该值指定了中间模型"。解决方案是一样的。