Django implements user Rights management based on Proxy

Source: Internet
Author: User

项目中经常会遇到用户权限管理的问题,django adminsite已经提供非常实用的用户权限管理机制。不过有些时候,我们希望根据相关用户属性来过滤adminsite中显示的内容。下文将结束如何实现:
Original class

Suppose I have such a questionnaire class, based on this class can be implemented to delete and change the function

class wenjuan(models.Model):    """    问卷    """    name=models.CharField(u'问卷名称',max_length=128)    breif=models.TextField(u'问卷简介')    zhichixinxi=models.TextField(u'支持信息',default=u'技术支持',null=True,blank=True)    danwei=models.ForeignKey(Department, verbose_name=u'所属单位')    status = models.SmallIntegerField(choices=WENJUAN_STATUS, verbose_name=u'状态')    fabu_time = models.DateTimeField(verbose_name=u'发布日期',null=True,blank=True)    jiezhi_time = models.DateTimeField(verbose_name=u'截止日期',null=True,blank=True)    create_time = models.DateTimeField(auto_now=True, verbose_name=u'创建日期',null=True,blank=True)    cjUser=models.ForeignKey(Users,related_name='users_cj',  verbose_name=u'所属用户',null=True,blank=True)    def __unicode__(self):        return self.name    class Meta:        verbose_name = u"问卷"        verbose_name_plural = verbose_name        #app_label = _(u'b')
Sub-class

Based on the sub-class implementation of different display according to permissions, in the demo project, we will implement the user can only query their own created questionnaires

class  chakanWenjuan(wenjuan):    class Meta:        proxy=True        verbose_name=_(u'统计结果')        verbose_name_plural=_(u'统计结果')

Xadminx, we need to set the Queryset filter

#-------------------------------------------------class chakanWenjuanAdmin(object):    list_display = ('name','danwei','cjUser',)    reversion_enable = True    search_fields = ['name',]    def queryset(self):        qs = super(chakanWenjuanAdmin, self).queryset()        if self.user.is_superuser:              return qs        else:            myuser=Users.objects.get(user__id=self.user.id)            return qs.filter(cjUser=myuser)xadmin.site.register(chakanWenjuan,chakanWenjuanAdmin)

If it's a superuser, it's all displayed. If you are a regular user, filter by user.

Notice

The next article describes how to customize columns in AdminSite

Django implements user Rights management based on Proxy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.