如何在Django中创建自定义权限(用户角色)?

时间:2021-12-08 19:57:43

I have simple question. How to create user role in Django without creating it in DB manually ?

我有一个简单的问题。如何在Django中创建用户角色而无需手动在DB中创建用户角色?

EDIT: I need to create specific permission. So only users with this permission can change specific field of model. I just need make one field 'readonly' for some users. Is it possible in django?

编辑:我需要创建特定权限。因此,只有拥有此权限的用户才能更改模型的特定字段。我只需要为某些用户创建一个“readonly”字段。 django有可能吗?

2 个解决方案

#1


7  

Creating a perm without using DB means you do not want to use django permission system. In that point, I would ask why you do not want to use it?

不使用数据库创建烫发意味着您不想使用django权限系统。在那一点上,我会问为什么你不想使用它?

If it is ok to use django permission sywstem, you can create a permission and check it by overrriding admin save method... Like:

如果可以使用django权限sywstem,则可以通过覆盖管理员保存方法来创建权限并进行检查...

in your models.py

在你的models.py中

class SomeModel(Model):
    your_spec_field = FieldType(...)
    class Meta:
        permissions = (
            ("some_perm", u"A name for your default permission"),
        )

In your related admin.py

在您的相关admin.py中

class SomeModelAdmin(ModelAdmin):
    def save_model(self, request, obj, form, change):
        if change:
            curr_value = SomeModel.objects.get(pk=obj.id)
            if not request.user.has_perm('<your_app>.some_perm'):
                obj.your_spec_field = curr_value.your_spec_field
        else:
            if not request.user.has_perm('<your_app>.some_perm')
                obj.your_spec_field = '' # or the default value according to your field type
        obj.save()

In this approach, you get the state of related record before you save it and check for related permission. If the user do not have required perm. then you replace your field's value with the previous one. If this is a new record, then you set that field to any value you want.

在此方法中,您将在保存相关记录之前获取相关记录的状态并检查相关权限。如果用户没有要求的烫发。然后用前一个字段替换字段的值。如果这是一条新记录,则将该字段设置为您想要的任何值。

#2


0  

You could check out django-authority. It lets you implement object level permissions, and even logical checks. Logical checks are not stored in the database, but are functions that can be passed values and return True or False. Shameless plug: my fork implements the django 1.2 object permission backend, so you could use it like: userA.hasperm("see_xyz")

你可以查看django-authority。它允许您实现对象级权限,甚至逻辑检查。逻辑检查不存储在数据库中,但是可以传递值并返回True或False的函数。无耻的插件:我的fork实现了django 1.2对象权限后端,所以你可以使用它:userA.hasperm(“see_xyz”)

#1


7  

Creating a perm without using DB means you do not want to use django permission system. In that point, I would ask why you do not want to use it?

不使用数据库创建烫发意味着您不想使用django权限系统。在那一点上,我会问为什么你不想使用它?

If it is ok to use django permission sywstem, you can create a permission and check it by overrriding admin save method... Like:

如果可以使用django权限sywstem,则可以通过覆盖管理员保存方法来创建权限并进行检查...

in your models.py

在你的models.py中

class SomeModel(Model):
    your_spec_field = FieldType(...)
    class Meta:
        permissions = (
            ("some_perm", u"A name for your default permission"),
        )

In your related admin.py

在您的相关admin.py中

class SomeModelAdmin(ModelAdmin):
    def save_model(self, request, obj, form, change):
        if change:
            curr_value = SomeModel.objects.get(pk=obj.id)
            if not request.user.has_perm('<your_app>.some_perm'):
                obj.your_spec_field = curr_value.your_spec_field
        else:
            if not request.user.has_perm('<your_app>.some_perm')
                obj.your_spec_field = '' # or the default value according to your field type
        obj.save()

In this approach, you get the state of related record before you save it and check for related permission. If the user do not have required perm. then you replace your field's value with the previous one. If this is a new record, then you set that field to any value you want.

在此方法中,您将在保存相关记录之前获取相关记录的状态并检查相关权限。如果用户没有要求的烫发。然后用前一个字段替换字段的值。如果这是一条新记录,则将该字段设置为您想要的任何值。

#2


0  

You could check out django-authority. It lets you implement object level permissions, and even logical checks. Logical checks are not stored in the database, but are functions that can be passed values and return True or False. Shameless plug: my fork implements the django 1.2 object permission backend, so you could use it like: userA.hasperm("see_xyz")

你可以查看django-authority。它允许您实现对象级权限,甚至逻辑检查。逻辑检查不存储在数据库中,但是可以传递值并返回True或False的函数。无耻的插件:我的fork实现了django 1.2对象权限后端,所以你可以使用它:userA.hasperm(“see_xyz”)