我们正在使用django开发一个客户管理应用程序,我们需要给代理设置权限,他/她是否可以编辑客户的属性()。
例如,
如果我有一个模型:
class Customer(models.Model):
# basic information
name = models.CharField(max_length=150) # the name of this customer
date = models.DateField(auto_now_add=True) # the date that this customer is created
# personal information
citizen_id = models.BigIntegerField(blank=True)
phone = models.BigIntegerField(max_length=20, blank=True)
work = models.CharField(max_length=100, blank=True)
address = models.CharField(max_length=300, blank=True)
bank_card = models.BigIntegerField()
# installation detail
primary = models.IntegerField(default=0)
secondary = models.IntegerField(default=0)
region = models.ForeignKey(Region) # the region that this customer currently lives in
type = models.ForeignKey(Type) # the installation type
group = models.ForeignKey(Group)
STATUS_CHOICES = (
('Active', 'Active'),
('Inactive', 'Inactive'),
('Transfered', 'Transfered'),
('Closed', 'Closed'),
('Suspended', 'Suspended'),
('Cancelled', 'Cancelled'),
)
status = models.CharField(max_length=40, choices=STATUS_CHOICES)
并且我希望能够设置编辑某些字段的权限,但是当前的权限系统只允许您添加/更改/删除模型实例,其中“更改”允许用户编辑所有属性在那个模型中,这不是我们真正想要的。
用户A可以编辑电话、地址、工作和citizen_id 用户 B 只能编辑电话和地址, 用户 C 可以编辑 citizen_id,..... 等等……
而且我希望能够设置不同的权限
这有可能实现吗?如果我可以使用 django 管理系统来管理代理和客户,那将非常有帮助。
======================= 非常感谢 FallenAngel 的回复。
我认为这正是我们想要的。
这是我试过的,
在 admin.py 中
class CustomerAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, extra_context=None):
agent = Agent.object.get(user=request.user)
permitted_fields = agent.permitted_fields # assume i have this setup...
self.readonly_fields = get_not_permitted_fields(premitted_fields) # assume I have this function written somewhere
return super(CustomerAdmin, self).change_view(request, object_id,
extra_context=None)
这正是我想要的方式:对于不允许的字段,将它们设置为只读...
再次感谢,
最佳答案
这是可能的...您必须使用 django 管理方法 as they described in here ...您必须定义 add_view、change_view 和 changelist_view 函数...
您还必须找到一种对用户进行分组的方法(分组可能是一种好方法,或者您可以使用权限)。我创建了一个扩展用户模型的模型,但您可以找到自己的方式...
其次编写基本的添加、更改和 ListView 功能,例如:
class CustomerAdmin(admin.ModelAdmin):
list_display= ["fields that will be used for all users"]
def changelist_view(self, request, extra_context=None):
if request.user == "type a":
self.list_display.extend["list of other fields"]
return super(CustomerAdmin, self).changelist_view(request, extra_context)
您必须指定添加、更改(和更改列表,如果需要) View 并处理每种用户类型。然后 Django 会为用户显示相关字段。您扩展 list_display、fileds、exclude、list_filter 和此类 django 管理字段显示方法...
关于django-admin - django 管理员修改模型属性的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021928/