简体   繁体   English

什么是模型的默认权限自动生成器?

[英]Whats the default permissions generator automatically of a model?

When I create a model: 创建模型时:

class Info(models.Model):
    name = models.CharField(max_length=26)
    detail = models.CharField(max_length=16)

    class Meta:
        permissions = (   
            ("add_info", "Can view info"),  # C
            ("read_info", "Can view info"),  # R
            ("update_info", "Can update info"),  # U
            ("delete_info", "Can delete info")  # D
        )

When I makemigrations, but there comes error: 当我进行迁移时,出现错误:

app07.Info: (auth.E005) The permission codenamed 'add_info' clashes with a builtin permission for model 'app07.Info'.
app07.Info: (auth.E005) The permission codenamed 'delete_info' clashes with a builtin permission for model 'app07.Info'.

So, when I create a user the model will create permissions automatically. 因此,当我创建用户时,模型将自动创建权限。 But you see, the READ and UPDATE permissions are not read_info and update_info , so whats the default permissions of a model? 但是您会看到, READUPDATE权限不是read_infoupdate_info ,那么模型的默认权限是什么?

From the permission document : 许可文件中

Default permissions 默认权限

When django.contrib.auth is listed in your INSTALLED_APPS setting, it will ensure that three default permissions – add, change and delete – are created for each Django model defined in one of your installed applications. 在INSTALLED_APPS设置中列出django.contrib.auth时,它将确保为在已安装的应用程序之一中定义的每个Django模型创建三个默认权限(添加,更改和删除)。

Assuming you have an application with an app_label foo and a model named Bar, to test for basic permissions you should use: 假设您有一个带有app_label foo和名为Bar的模型的应用程序,要测试基本权限,应使用:

add: user.has_perm('foo.add_bar')
change: user.has_perm('foo.change_bar')
delete: user.has_perm('foo.delete_bar')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM