How to filter objects by user id with tastypie?
2015-10-30 10:27
302 查看
I have the following user resource:
class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name = 'user' fields = ['username', 'first_name', 'last_name'] allowed_methods = ['get'] filtering = { 'username': ALL, 'id': ALL, }and the following model resource:
class GoalResource(ModelResource): user = fields.ForeignKey(UserResource, 'user') class Meta: #authentication = BasicAuthentication() #authorization = ReadOnlyAuthorization() queryset = Goal.objects.all() resource_name = 'goal' filtering = { 'user': ALL_WITH_RELATIONS, }I want to be able to filter the goal by user id rather than username.I can get a list of goals from certain usernames by doing a GET request on this:
http://localhost:8000/api/v1/goal/?user__username=testBut I want to be able to sort by user id instead:
http://localhost:8000/api/v1/goal/?user__id=1How would I get the second part to work?Also, what is the general procedure for accessing a currently logged in user's id through Javascript? I am using backbonejs, and I want to do a post for all of a logged in user's goal. I thought about putting a hidden field on the page with the user's id. Thenextracting the value of the hidden field from the DOM, but I figured it's easy to use chrome's developer tools to change the id whenever I want. Of course, I will use authentication to check that the logged in user's id matches the one that I extract fromthe hidden field, though. But what is the accepted way?
Answers
I am not sure if what I propose here can work in your authorization. It works for me using ApiKeyAuthorization and Authorization.I read the idea from: http://django-tastypie.readthedocs.org/en/latest/cookbook.html [Section:Creating per-user resources ]My suggestion is:What about uncommenting authentication and authorization, and overriding obj_create and apply_authorization. I am using that in my project, and it works. In the code of the method apply_authorization, I just added the if condition checking for superuser, youcan just return the object_list+filter without checking that (I do it cause if is not superuser, I return data related to groups of users).class GoalResource(ModelResource): user = fields.ForeignKey(UserResource, 'user') class Meta: authentication = BasicAuthentication() authorization = ReadOnlyAuthorization() queryset = Goal.objects.all() resource_name = 'goal' filtering = { 'user': ALL_WITH_RELATIONS, } def obj_create(self, bundle, request=None, **kwargs): return super(EnvironmentResource, self).obj_create(bundle, request, user=request.user) def apply_authorization_limits(self, request, object_list): if request.user.is_superuser: return object_list.filter(user__id=request.GET.get('user__id',''))Hope is what you were asking, and it helps. best with that!Note - apply_authorization_limits is deprecated.The alternative way to filter by the current user, is to override read_list in you authorization class. This is what I have. My class overrides DjangoAuthorization.
def read_list(self, object_list, bundle): klass = self.base_checks(bundle.request, object_list.model) if klass is False: return [] # GET-style methods are always allowed. # Filter by user if not hasattr(bundle.request, 'user'): return None object_list = object_list.filter(user__id=bundle.request.user.id) return object_list
http://kksvip.com/questions/1385498/how-to-filter-objects-by-user-id-with-tastypie
相关文章推荐
- js判断object里面是否包含某一字段
- objective-c 绘图资料
- Node.js 中开源库探秘 object-assign | 全栈之路
- 提升Object-C代码质量
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- Objective-C的属性和成员变量用法及关系浅析
- 详谈OC(object-c)深浅复制/拷贝-什么情况下用retain和copy
- [Object C]_[初级]_[两个数组共有元素的提取,删除,数组的合并]
- iOS SDK开发时,有关OC和C++混编造成其它类错误的问题(Compile Sources As Objective-C++)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- iPhone How-to:解析URL中的键值对
- 《从C++到Objective-C》看Objective-C
- Objective-C学习笔记类目、协议
- Python Exception Objects
- 【Objective-C Runtime】之理解
- 一些常用的工具类方法
- objective学习笔记1
- Ljava.lang.Object;@ba8a1dc 问题
- Objective-c学习笔记之集合
- NSObject 排序