如何根据当前用户使用好吃的东西进行自动过滤 [英] How to do automatic filtering based on the current user with tastypie

查看:94
本文介绍了如何根据当前用户使用好吃的东西进行自动过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过DjangoAuthorization方法使用tastypie.

I'm using tastypie with the DjangoAuthorization method.

我有一个像这样的StudentResource:

I have a StudentResource like this :

class StudentResource(ModelResource):
  friends = fields.ToManyField(StudentResource, 'friends', null=True)

  class Meta:
    queryset = Student.objects.all()
    resource_name = 'student'
    authorization = DjangoAuthorization()

所以我的每个学生都有很多朋友.

So each of my student has many friends.

现在,当我的用户仅对他的朋友进行API调用时,我想返回. (基于他的django ID). (我不想只是向我的资源添加过滤器,我真的希望用户只能访问他的朋友)

Now, I'd like to return, when my user is making an API call only his friends. (based on his django id). (I don't want to just add a filter to my Ressource, I really want the user to be only able to access to his friends)

我可以使用get_list tastypie函数覆盖GET方法,但这看起来很难看.

I could override the GET method using the get_list tastypie function, but it seems pretty ugly.

那么做这件事的好方法是什么?

So what is the good way to do that ?

谢谢!

推荐答案

实际上,这样做的好方法是为StudentResource创建自定义授权.

Actually, the good way to do that is to create a custom Authorization for the StudentResource.

这是好吃的文档,它解释说: http://django-tastypie.readthedocs .org/en/latest/authorization.html

Here's the tastypie doc explaining that : http://django-tastypie.readthedocs.org/en/latest/authorization.html

这篇关于如何根据当前用户使用好吃的东西进行自动过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆