在django中添加自定义用户权限

时间:2021-02-16 19:21:29

i have the following code in my models.py

我在models.py中有以下代码

from django.db import models

# Create your models here.
class LabName(models.Model):
    labsname=models.CharField(max_length=30,unique=True)
    #room_number=models.CharField(max_length=3)
    def __unicode__(self):
     return self.labsname
STAT=(('W','Working'),('N','Not Working'))
class ComponentDescription(models.Model):
    lab_Title=models.ForeignKey('Labname')
    component_Name = models.CharField(max_length=30)
    description = models.TextField(max_length=200)
    qty = models.IntegerField()
    purchased_Date = models.DateField()
    status = models.CharField(max_length=1,choices=STAT)
    to_Do = models.CharField(max_length=30,blank=True) 
    remarks = models.CharField(max_length=30)


    def __unicode__(self):
        return self.component_Name

i have the following in my admin.py

我的admin.py中有以下内容

from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName

class ComponentDescriptionInline(admin.TabularInline):
    model = ComponentDescription
    extra=0

class LabNameAdmin(admin.ModelAdmin):
        inlines = [
        ComponentDescriptionInline,
    ]

class ComponentDescriptionAdmin(admin.ModelAdmin):
    list_display=('lab_Title','component_Name','description','qty','purchased_Date','status','to_Do','remarks')
        list_filter=('lab_Title','status','purchased_Date')
admin.site.register(LabName, LabNameAdmin)
admin.site.register(ComponentDescription,ComponentDescriptionAdmin)

i want to assign users with custom privilages .i mean that different users can modify only the labs that they are assigned with.Django admin allows to add edit lab permissions i.e edit,delete and modify labs.but The problem is that every user can access all the labs

我想为用户分配自定义权限.i意味着不同的用户只能修改他们分配的实验室.Django管理员允许添加编辑实验室权限,即编辑,删除和修改labs.but问题是每个用户都可以访问所有的实验室

1 个解决方案

#1


1  

If I understand the question correctly, you are trying to control permissions per object.

如果我正确理解了这个问题,那么您正在尝试控制每个对象的权限。

This should help: Adding per-object permissions to django admin

这应该有所帮助:向django admin添加每个对象的权限

I've also heard that you can do this with the django-guardian package, although I have never tried it: http://pythonhosted.org/django-guardian/

我也听说你可以用django-guardian包来做到这一点,虽然我从未尝试过:http://pythonhosted.org/django-guardian/

#1


1  

If I understand the question correctly, you are trying to control permissions per object.

如果我正确理解了这个问题,那么您正在尝试控制每个对象的权限。

This should help: Adding per-object permissions to django admin

这应该有所帮助:向django admin添加每个对象的权限

I've also heard that you can do this with the django-guardian package, although I have never tried it: http://pythonhosted.org/django-guardian/

我也听说你可以用django-guardian包来做到这一点,虽然我从未尝试过:http://pythonhosted.org/django-guardian/