Django Tastypie - 自动获取当前用户资源

时间:2012-11-02 08:50:12

标签: django tastypie

在我的帖子方法中,我想自动填充“所有者”#39;当前用户的字段(我使用BasicAuthentication)。我在这里遵循了一些代码:How to get authorized user object in django-tastypie - 但我还没有完全正常工作。

resources.py:

class QuestionResource(ModelResource):
    owner = fields.ForeignKey(UserResource, 'owner')
    class Meta:
        queryset = Question.objects.all()
        allowed_methods = ['get','post']
        fields = ['title','type']
        resource_name = 'question'
        include_resource_uri = True
        serializer = PrettyJSONSerializer()
        authentication = BasicAuthentication()
        authorization = Authorization()

    def obj_create(self, bundle, request=None, **kwargs):
        bundle = self.full_hydrate(bundle, request)
        return bundle

    def obj_update(self, bundle, request=None, **kwargs):
        bundle = self.full_hydrate(bundle, request)
        return bundle

    def full_hydrate(self, bundle, request=None):
        bundle = self.hydrate(bundle, request)
        return bundle

    def hydrate(self, bundle, request=None):
        bundle.obj.owner = User.objects.get(pk = request.user.id)
        return bundle

如果我发出以下内容:

curl -u USERNAME:XXXXXXX --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"title\":\"my new question\",\"type\":\"multichoice\"}" http://localhost/python/mquiz/api/v1/question/

我收到回复:

HTTP/1.1 201 CREATED
Date: Fri, 02 Nov 2012 08:28:05 GMT
Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Language,Cookie,Accept-Encoding
Location: http://localhost/python/mquiz/api/v1/question/None/
Content-Language: en-us
Content-Length: 0
Content-Type: text/html; charset=utf-8

所以好像它已经全部工作了,但是没有添加到数据库中,并且位置看起来不对(最后是/ None /)。

我确定它与该行" bundle.obj.owner = User.objects.get(pk = request.user.id)"有关。如果我使用" bundle.obj.owner = request.user.id"然后我得到错误:

"error_message": "Cannot assign \"3L\": \"Question.owner\" must be a \"User\" instance."

据我所知,bundle.obj.owner需要采用以下形式:' / python / mquiz / api / v1 / user / 3 /' - 如果我在我的curl请求中使用它作为所有者参数(并删除我的自定义水合物方法),那么一切正常并被添加到数据库中。那么如何将我的User实例转换为可以接受的形式呢?

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:4)

啊,啊......现在明白了。有用的:

class QuestionResource(ModelResource):
    owner = fields.ForeignKey(UserResource, 'owner')
    class Meta:
        queryset = Question.objects.all()
        allowed_methods = ['get','post']
        fields = ['title','type']
        resource_name = 'question'
        include_resource_uri = True
        serializer = PrettyJSONSerializer()
        authentication = BasicAuthentication()
        authorization = Authorization()


    def hydrate(self, bundle, request=None):
        bundle.obj.owner = User.objects.get(pk = bundle.request.user.id)
        return bundle 

我需要bundle.request.user.id而不仅仅是request.user.id。也许我正在看的另一个问题是提到了旧版本的TastyPie - 似乎旧版本没有可以在捆绑中访问请求?

答案 1 :(得分:1)

当前验证的答案是次优的,因为它会产生2个SQL查询。

您应该修改您的查询集,如下所示:

queryset = Question.objects.select_related('owner').all()

这样的主人'数据在一个SQL查询中连接。