【发布时间】:2019-05-05 19:29:37
【问题描述】:
我正在尝试在后端创建组和权限。
现在我试图了解content_type 参数是什么以及在创建权限时如何使用它。
Documentation for Permission model says:
content_type¶
必填。对 django_content_type 数据库表的引用,其中包含每个已安装模型的记录。
我怎样才能得到这个 content_type?我应该在哪里寻找它?
我使用 PosgresSQL 作为数据库。
根据this other question,可以这样做:
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get(app_label='app_name', model='model_name')
permission = Permission.objects.create(codename='can_create_hr',
name='Can create HR',
content_type=content_type) # creating permissions
group = Group.objects.get(name='HR')
group.permissions.add(permission)
但同样,app_label='app_name', model='model_name' 里面是什么:content_type = ContentType.objects.get(app_label='app_name', model='model_name')?
我的项目结构:
stickers-gallito-app
|_cart
|_shop
【问题讨论】:
标签: django