如何在django-tastypie中获取授权的用户对象 [英] How to get authorized user object in django-tastypie

查看:89
本文介绍了如何在django-tastypie中获取授权的用户对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类SalepointResource(ModelResource):
def hydrate(self,bundle):
user = bundle.request.user

但是,请求是空的 HttpRequest 对象,虽然用户被授权,但没有用户方法。有没有办法获得用户对象?

解决方案

使用TastyPie 0.9.15,我发现这是有用的:

  def hydrate_user(self,bundle):
bundle.obj.user = bundle.request.user
return bundle

不需要子类化 ModelResource 。这里用户是模型和资源的 ForeignKey
我发布这个作为答案,因为虽然看起来很简单,花了我很长时间才能弄清楚。


I need to get authorized user object in hydrate method, something like that:

class SalepointResource(ModelResource):
  def hydrate(self, bundle):
    user = bundle.request.user

But request here is empty HttpRequest object, and doesn't have user method, although user is authorized. Is there any way to get user object?

解决方案

With TastyPie 0.9.15, I find this works:

def hydrate_user(self, bundle):
    bundle.obj.user = bundle.request.user
    return bundle

with no need for subclassing ModelResource. Here user is a ForeignKey of the model and the resource. I'm posting this as an answer because although it looks simple, it took me a long time to figure out.

这篇关于如何在django-tastypie中获取授权的用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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