鉴于这些模型:
class ModelA(models.Model):
# Some fields
# Some relationship
class ModelB(models.Model):
# Some fields
a = models.ForeignKey(ModelA)
和这些tastypie资源:
class ResourceA(ModelResource):
b_list = fields.ToManyField('app.api.ResourceB', 'a_set', null=True, related_name='a')
class ResourceB(ModelResource):
a = fields.ToOneField(ResourceA, 'a', null=True)
如果我访问ResourceB的详细端点(/ api / v1 / resourceA / ## /),我可以看到所有相关的B的uris - 反向关系有效。返回的数据如下所示:
{
'b_list': [
'/api/v1/resourceB/1/',
'/api/v1/resourceB/2/',
]
}
如果我尝试对资源A进行PUT或PATCH以向其添加新的B,请使用此数据:
{
'b_list': [
'/api/v1/resourceB/1/',
'/api/v1/resourceB/2/',
'/api/v1/resourceB/3/',
]
}
我收到错误:'ManyRelatedManager' object has no attribute 'add'
此堆栈跟踪:
Traceback (most recent call last):
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 202, in wrapper
response = callback(request, *args, **kwargs)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 449, in dispatch_detail
return self.dispatch('detail', request, **kwargs)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 472, in dispatch
response = method(request, **kwargs)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1611, in patch_detail
self.update_in_place(request, bundle, deserialized)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1634, in update_in_place
return self.obj_update(bundle=original_bundle, **kwargs)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2166, in obj_update
return self.save(bundle, skip_errors=skip_errors)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2257, in save
self.save_m2m(m2m_bundle)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2382, in save_m2m
related_resource.save(updated_related_bundle)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2257, in save
self.save_m2m(m2m_bundle)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2382, in save_m2m
related_resource.save(updated_related_bundle)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2257, in save
self.save_m2m(m2m_bundle)
File \"/home/username/.virtualenvs/myapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2385, in save_m2m
related_mngr.add(*related_objs)
AttributeError: 'ManyRelatedManager' object has no attribute 'add'
如果我立即 PUT / PATCH相同的数据,我没有错误,没有堆栈跟踪,并且项目已成功添加到关系中。我无法弄清楚为什么会这样。
另外,我似乎只能在关系中添加项目 - 能够删除它们也很棒。我可能会问另一个问题。
答案 0 :(得分:0)
确定。这是一个Django错误。 我不确定,尝试在django模型中使用related_name。
class ModelB(models.Model):
# Some fields
a = models.ForeignKey(ModelA, related_name='b_list')
从资源中删除related_name,属性名称必须从“a_set”更改为“b_list”
class ResourceA(ModelResource):
b_list = fields.ToManyField('app.api.ResourceB', 'b_list', null=True)