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 に答える