Tastypie“无法将关键字u'request'解析为字段...”错误

时间:2014-09-22 18:36:24

标签: django tastypie

我看到了错误

  

"无法解析关键字u' request'进入领域..."

当我尝试通过HTTP PATCH更新Tastypie实现的TagResource时。但是,如果我在我的资源中覆盖obj_update(),它就可以工作(见下文)。

我在Django V1.6.6和Tastypie V0.11.1上。

urls.py:

...
v1_api = Api(api_name='v1')
...

资源:

class TagResource(ModelResource):

    user = fields.ForeignKey(UserResource, 'user')

    ''' xxx works when this is uncommented-out
    def obj_update(self, bundle, skip_errors=False, **kwargs):
        bundle.obj = self.get_via_uri(bundle.data.get('resource_uri'), bundle.request)
        print "OBJECT WAS FETCHED" # HERE and confirm the above worked
        bundle = self.full_hydrate(bundle)
        print "HYDRATED!!!"
        return self.save(bundle, skip_errors=skip_errors)
    '''

    class Meta:
        queryset = Tag.objects.all()
        resource_name = 'tag'
        fields = ['tagid', 'short_code', 'name', 'desc', 'user', 'created', 'modified']
        allowed_methods = ['get', 'post', 'patch', 'put']
        authentication = Authentication()
        authorization = Authorization()

模型:

class Tag(models.Model):

    tagid = models.CharField(max_length=100)
    short_code = models.CharField(max_length=100)
    name = models.CharField(max_length=100)
    desc = models.CharField(max_length=1000)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

表架构:

sqlite> .schema api_v01_tag
CREATE TABLE "api_v01_tag" (
    "id" integer NOT NULL PRIMARY KEY,
    "tagid" varchar(100) NOT NULL,
    "short_code" varchar(100) NOT NULL,
    "name" varchar(100) NOT NULL,
    "desc" varchar(1000) NOT NULL,
    "user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
    "created" datetime NOT NULL,
    "modified" datetime NOT NULL
);
CREATE INDEX "api_v01_tag_6340c63c" ON "api_v01_tag" ("user_id");

资源条目:

sqlite> select * from api_v01_tag;
0|ABC|abc||A new description3.|1|2014-09-17 23:20:56|2014-09-22 18:01:27.741707

curl命令和服务器响应:

bash-3.2$ curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"desc": "A new description3."}' http://localhost:8000/api/v1/tag/0/

HTTP/1.0 500 INTERNAL SERVER ERROR
Date: Mon, 22 Sep 2014 18:02:29 GMT
Server: WSGIServer/0.1 Python/2.7.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Content-Type: application/json
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE,PATCH

{"error_message": "Cannot resolve keyword u'request' into field. Choices are: created, desc, id, modified, name, pic, short_code, tagid, user", "traceback": "Traceback (most recent call last):

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 201, in wrapper
    response = callback(request, *args, **kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 441, in dispatch_detail
    return self.dispatch('detail', request, **kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 464, in dispatch
    response = method(request, **kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 1625, in patch_detail
    self.update_in_place(request, bundle, deserialized)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 1648, in update_in_place
    return self.obj_update(bundle=original_bundle, **kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 2164, in obj_update
    bundle.obj = self.obj_get(bundle=bundle, **lookup_kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/tastypie/resources.py\", line 2082, in obj_get
    object_list = self.get_object_list(bundle.request).filter(**kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/query.py\", line 593, in filter
    return self._filter_or_exclude(False, *args, **kwargs)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/query.py\", line 611, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1204, in add_q
    clause = self._add_q(where_part, used_aliases)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1240, in _add_q
    current_negated=current_negated)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1103, in build_filter
    allow_explicit_fk=True)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1363, in setup_joins
    names, opts, allow_many, allow_explicit_fk)

  File \"/Users/yea/env0/lib/python2.7/site-packages/django/db/models/sql/query.py\", line 1283, in names_to_path
    \"Choices are: %s\" % (name, \", \".join(available)))

FieldError: Cannot resolve keyword u'request' into field. Choices are: created, desc, id, modified, name, pic, short_code, tagid, user
"}

0 个答案:

没有答案