0
class TestResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Test.objects.all()
        resource_name = 'test'
        authorization = Authorization()
        authentication = BasicAuthentication()

如何获取Test实际登录用户创建的对象?

所有对象:

http://127.0.0.1:8000/api/test/?format=json

4

1 回答 1

0

假设您要根据当前登录的用户返回资源,您可以通过覆盖资源来限制返回apply_authorization_limits的资源。为 POST 设置当前用户与覆盖obj_create.

有关示例,请参见:http ://django-tastypie.readthedocs.org/en/latest/cookbook.html#creating-per-user-resources。

class TestResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

    class Meta:
        queryset = Test.objects.all()
        resource_name = 'test'
        authorization = Authorization()
        authentication = BasicAuthentication()

    def obj_create(self, bundle, **kwargs):
        return super(TestResource, self).obj_create(bundle,
            user=bundle.request.user)

    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(user=request.user)
于 2013-10-10T12:04:52.350 回答