4

我正在开发一个 API,对于我的一个端点,我希望能够进行部分更新。

这里是美味的资源

class StoryResource(ModelResource):
    authors = fields.ToManyField(SimpleAuthorResource, 'authors', full=True)
    posts = fields.ToManyField(SimplePostResource, 'posts', full=True, blank=True)
    cover_photo = fields.ForeignKey(PhotoResource, 'cover_photo', full=True)
    class Meta:
        queryset = Story.objects.all()
        resource_name = 'story'
        validation = ModelFormValidation(form_class=StoryForm)
        authorization = Authorization()
        allowed_methods = ['get', 'post', 'patch', 'put']
        ordering = ['-created_ts']
    def determine_format(self, request):
        return "application/json"

我正在使用 POSTMAN 发出 PATCH 请求以更新 Story 模型中的字段。

它返回此错误:

{"error_message": "'Bundle' 对象不可迭代", "traceback": "Traceback (最近一次调用最后一次):\n\n 文件\"/usr/local/lib/python2.7/dist-packages/ sweetpie/resources.py\",第 192 行,在包装器中\n response = callback(request, *args, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/ sweetpie/resources.py\",第 406 行,在 dispatch_detail\n return self.dispatch('detail', request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist- packages/tastypie/resources.py\",第 427 行,在 dispatch\n response = method(request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie /resources.py\",第 1332 行,在 patch_detail\n self.update_in_place(request, bundle, deserialized)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\",第 1345 行,在 update_in_place\n self.is_valid(original_bundle, request)\n\n 文件 \"/usr/local/lib/python2.7/dist-packages /tastypie/resources.py\",第 991 行,在 is_valid\n 中错误 = self._meta.validation.is_valid(bundle, request)\n\n 文件 \"/var/www/novella-django/novella/novella/ api/validation.py\",第 55 行,在 is_valid\n data[field] = self.uri_to_pk(data[field])\n\n File \"/var/www/novella-django/novella/novella/api /validation.py\",第 29 行,在 uri_to_pk\n for one_uri in uris:\n\nTypeError: 'Bundle' object is not iterable\n"}在 is_valid\n 错误 = self._meta.validation.is_valid(bundle, request)\n\n 文件 \"/var/www/novella-django/novella/novella/api/validation.py\",第 55 行,在is_valid\n data[field] = self.uri_to_pk(data[field])\n\n 文件 \"/var/www/novella-django/novella/novella/api/validation.py\",第 29 行,在 uri_to_pk \n for one_uri in uris:\n\nTypeError: 'Bundle' object is not iterable\n"}在 is_valid\n 错误 = self._meta.validation.is_valid(bundle, request)\n\n 文件 \"/var/www/novella-django/novella/novella/api/validation.py\",第 55 行,在is_valid\n data[field] = self.uri_to_pk(data[field])\n\n 文件 \"/var/www/novella-django/novella/novella/api/validation.py\",第 29 行,在 uri_to_pk \n for one_uri in uris:\n\nTypeError: 'Bundle' object is not iterable\n"}

我不太确定出了什么问题,而且我似乎在其他任何地方都找不到这个错误。

4

1 回答 1

4

听起来您的验证正在尝试使用以下行遍历您的外键包:

for one_uri in uris:

这就是它的"Bundle" object is not iterable来源。如果您想将这些字段作为 resource_uris 进行迭代,请full=True从这些 FK 字段中删除。


如果要将它们保留为full=True,则需要更新验证以处理这些字段的捆绑包或通过exclude在表单元类中使用将它们从验证中排除:

class ModelFormValidation:
    ...

    class Meta:
        exclude = (
            authors,
            posts,
            cover_photo            
        )
于 2012-11-09T16:03:08.970 回答