[英]Django - How to work with `auth_permission` Model
Django make its own permission models, like auth_permission
. Django建立自己的权限模型,例如
auth_permission
。
I want to add new permission doing this by code. 我想通过代码添加新的权限。
How I normally add something to a table 我通常如何在表中添加内容
from .models import Model
mdl, succeeded = Model.objects.get_or_create(
field=value,
field=value
)
mdl.save()
But to call the auth_permission
Model, I have to import the model first. 但是要调用
auth_permission
模型,我必须先导入模型。
Only I can't find how this Model is called. 只有我找不到此模型的调用方式。
So do one of you guys know what to import to call this Model? 那么,你们中的一个人知道导入什么来调用此模型吗?
You can import the permission model with 您可以导入权限模型
from django.contrib.auth.models import Permission
However, if you want to create custom permissions, the documented approach is to add permissions to a model. 但是,如果要创建自定义权限,则记录的方法是向模型添加权限。 Django will then create the permissions when you run
manage.py migrate
. 然后,当您运行
manage.py migrate
时,Django将创建权限。
class MyModel(models.Model):
...
class Meta:
permissions = (
("can_do_something", "Can do something"),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.