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
假设您要基于当前登录的用户返回资源,则可以通过覆盖资源来限制返回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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句