默認情況下,每個django模型有3個權限(添加,更改,刪除)。在模型中,我可以定義自定義權限以添加更多內容。如何在django中檢索特定模型的所有權限?
class Company(models.Model):
owner = models.ForeignKey(User)
name = models.CharField(max_length=64, unique=True)
description = models.TextField(max_length=512)
created_on = models.DateTimeField(auto_now_add=timezone.now)
class Meta:
permissions = (
("erp_view_company", "Can see the company information"),
("erp_edit_company", "Can edit the company information"),
("erp_delete_company", "Can delete the company"),
)
當您遷移時,這些權限會自動創建在數據庫級別。如何從模型中檢索所有權限?
# retrieves the permissions
permissions = Permission.objects.filter(get_all_permissions_of_model_Company)
# adds permissions to group
group = Group.objects.create(name='foo', permissions=permissions)
# adds user to group
user.groups.add(group)
好的,謝謝你的支持! – realnot