Django用户会话混合。 [英] Django User's Sessions getting Mixed.

查看:145
本文介绍了Django用户会话混合。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django的管理界面中,有一个表单,一个特定的模型,我想以不同的方式渲染,这取决于访问此表单的用户是正常用户还是管理员。除了一件巨大的事情,它的工作发现:它混合了。例如,管理员可以编辑的字段是只读的,在我之前,我已经以正常的用户身份登录了。如果我重新启动服务器后立即尝试完全相同的事情,工作正常,所有字段都是可编辑的。我猜想用户的会话会混乱。如何处理这个?



这是我有的:



在App的admin.py:

  151 class AdminForm(ModelForm):
152 class Meta:
153 model = PromotionalSlot
154 exclude = ['deal']
155
156 class NormalUserForm(ModelForm):
157 class Meta:
158 model = PromotionalSlot
159 exclude = ['site' ]
160
161 class PromotionalSlotAdmin(admin.ModelAdmin):
162 def get_form(self,request,obj = None,** kwargs):
163 if request.user。 is_superuser:
164 return AdminForm
165 else:
166 self.readonly_fields = ['start_date','end_date','level','city','status']
167 return NormalUserForm·
168
169 admin.site.register(models.PromotionalSlot,PromotionalSlotAdmin)

谢谢

解决方案

问题是您的解决方案不是线程安全的。 Django会保留管理实例,因此您设置为 self.readonly_fields 的值也用于线程中的所有后续请求。这是如何使线程安全的:

  class PromotionalSlotAdmin(admin.ModelAdmin):

def get_readonly_fields(self,request,obj = None):
如果request.user.is_superuser:
return []
else:
return ['start_date','end_date' 'level','city','status']

def get_form(self,request,obj = None,** kwargs):
if request.user.is_superuser:
返回AdminForm
其他:
返回NormalUserForm


In Django's admin interface, there is a form, of a specific model that I want to be rendered in different ways depending if the logged user accessing this form is a normal user or an admin. It's working find except one HUGE thing: it get's mixed. For instance, the fields that should be editable by the admin are read-only, after I immediately prior to that had been logged in as a "normal" user. If I try the exactly same thing immediately after restarting the server, works fine and all fields are editable. I guess that somehow the user's sessions are get mixed up. How to handle this?

Here is waht I have:

In the App's admin.py:

151 class AdminForm(ModelForm):
152     class Meta:
153         model = PromotionalSlot
154         exclude = ['deal']
155 
156 class NormalUserForm(ModelForm):
157     class Meta:
158         model = PromotionalSlot
159         exclude = ['site']
160         
161 class PromotionalSlotAdmin(admin.ModelAdmin):
162      def get_form(self, request, obj=None, **kwargs):
163         if request.user.is_superuser:
164             return AdminForm
165         else:
166             self.readonly_fields = ['start_date','end_date','level','city','status']
167             return NormalUserForm· 
168
169 admin.site.register(models.PromotionalSlot, PromotionalSlotAdmin)

Thanks

解决方案

The problem is that your solution isn't thread safe. Django keeps the admin instance around and so the value you set to self.readonly_fields is used also for all subsequent requests in the thread. This is how you can make it thread safe:

class PromotionalSlotAdmin(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        if request.user.is_superuser:
            return []
        else:
            return ['start_date', 'end_date', 'level', 'city', 'status']

    def get_form(self, request, obj=None, **kwargs):
        if request.user.is_superuser:
            return AdminForm
        else:
            return NormalUserForm

这篇关于Django用户会话混合。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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