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

时间:2013-03-11 19:31:32

标签: django django-models django-admin django-views

我的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

我的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意味着不同的用户只能修改他们分配的实验室.Django admin允许添加编辑实验室权限,即编辑,删除和修改labs.but问题是每个用户可以访问所有实验室

1 个答案:

答案 0 :(得分:1)

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

这应该有所帮助: Adding per-object permissions to django admin

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