使用Tastypie保存M2M字段

时间:2013-07-18 09:46:25

标签: django tastypie

我正在使用Tastypie构建API,并且在保存多对多字段时遇到了一个问题。

我有一个名为Pest的模型调用,另一个名为Call,而Call有一个名为pests的字段,代表可以应用于呼叫的害虫。这些已经存在,用户可以选择一个或多个应用于该调用 - 无意与Call对象同时创建它们。

默认情况下,当我尝试通过POST创建新的呼叫时出现以下错误:

{"error_message": "Cannot resolve keyword 'url' into field. Choices are: baitpoint, call, description, id, name, operator", "traceback": "Traceback (most recent call last):\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 217, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 459, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 491, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1357, in post_list\n    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2150, in obj_create\n    return self.save(bundle)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2300, in save\n    m2m_bundle = self.hydrate_m2m(bundle)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 964, in hydrate_m2m\n    bundle.data[field_name] = field_object.hydrate_m2m(bundle)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/fields.py\", line 853, in hydrate_m2m\n    m2m_hydrated.append(self.build_related_resource(value, **kwargs))\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/fields.py\", line 653, in build_related_resource\n    return self.resource_from_uri(self.fk_resource, value, **kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/fields.py\", line 573, in resource_from_uri\n    obj = fk_resource.get_via_uri(uri, request=request)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 838, in get_via_uri\n    return self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2125, in obj_get\n    object_list = self.get_object_list(bundle.request).filter(**kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/django/db/models/query.py\", line 655, in filter\n    return self._filter_or_exclude(False, *args, **kwargs)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/django/db/models/query.py\", line 673, in _filter_or_exclude\n    clone.query.add_q(Q(*args, **kwargs))\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1266, in add_q\n    can_reuse=used_aliases, force_having=force_having)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1134, in add_filter\n    process_extras=process_extras)\n\n  File \"/home/matthew/Projects/Pestability/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1332, in setup_joins\n    \"Choices are: %s\" % (name, \", \".join(names)))\n\nFieldError: Cannot resolve keyword 'url' into field. Choices are: baitpoint, call, description, id, name, operator\n"}

所以我看了一下,发现了this answer,这似乎涵盖了类似的情况。我将hydrate_pests方法添加到CallResource类中,如下所示:

class AbstractModelResource(ModelResource):
    class Meta:
        authorization = DjangoAuthorization()
        authentication = ApiKeyAuthentication()
        cache = SimpleCache(timeout=10)
        always_return_data = True


class FilteredByOperatorAbstractModelResource(AbstractModelResource):
    def authorized_read_list(self, object_list, bundle):
        user = bundle.request.user
        site_user = SiteUser.objects.get(user=user)
        return object_list.filter(operator=site_user.operator)


class PestResource(FilteredByOperatorAbstractModelResource):
    class Meta(AbstractModelResource.Meta):
        queryset = Pest.objects.all()
        resource_name = 'pest'
        allowed_methods = ['get']


class CallResource(AbstractModelResource):
    client = fields.ForeignKey(ClientResource, 'client')
    operator = fields.ForeignKey(OperatorResource, 'operator')
    pests = fields.ManyToManyField(PestResource, 'pests', null=True)

    class Meta(AbstractModelResource.Meta):
        queryset = Call.objects.all()
        resource_name = 'call'

    def hydrate_pests(self, bundle):
        pests =  bundle.data.get('pests', [])
        pest_ids = []
        for pest in pests:
            m = re.search('\/api\/v1\/pests\/(\d+)\/', str(pest))
            try:
                id = m.group(1)
                pest_ids.append(id)
            except AttributeError:
                pass

        bundle.data['pests'] = Pest.objects.filter(id__in=pest_ids)
        return bundle

pests字段的传递方式如下:

0: "/api/v1/pests/6/"
1: "/api/v1/pests/7/"

当我运行bundle.data.get('pests', [])时,有害生物URL正确显示 - 如果我使用PDB设置跟踪,我可以验证URL是否正在通过,Pest.objects.filter(id__in=pest_ids)正在返回正确的项目。但是,尽管HTTP POST请求成功,但未更新有害生物字段以反映新数据。

谁能看到我出错的地方?我是否正确地将Pest对象列表传递给bundle.data ['pests'],或者这不是我应该如何将这些数据传递到该字段?

实际传递给bundle.data的内容如下:

{'pests': [<Pest: Rats>, <Pest: Mice>], 'notes': u'Blah', 'first_choice_visit_time': u'2013-07-18T02:02', 'client': u'/api/v1/client/28/', 'date': u'2013-07-18', 'second_choice_visit_time': u'2014-03-03T03:02'}

1 个答案:

答案 0 :(得分:2)

捆绑数据包含字典。您正在向它传递QuerySet对象列表。尝试将.values()附加到您的查询集。