回答问题 #1:Tastypie 文档描述了如何创建每个用户的资源。假设用户已经是请求的一部分:
class MyResource(ModelResource):
class Meta:
queryset = MyModel.objects.all()
resource_name = 'environment'
list_allowed_methods = ['get', 'post']
authentication = ApiKeyAuthentication()
authorization = Authorization()
# Only allow creation of objects belonging to the user
def obj_create(self, bundle, **kwargs):
return super(EnvironmentResource, self).obj_create(bundle, user=bundle.request.user)
# Only allow accessing resources for this user
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
要回答问题 #2,您可能应该让用户成为会话的一部分。