Django Content type table - Auth Permission

随声附和 提交于 2019-12-01 13:31:40

"content_type_id" is the name of the field as determined by Django.

The field is a foreign key relation ship to an instance of ContentType.

The importance of this field is that it designates the object to which the permission applies (from the django source code 1.7.11):

Permissions are set globally per type of object, not per specific object instance. It is possible to say "Mary may change news stories ...

In the above quote, the content type field would be a foreign key relation to "new story".

In your use case, you want to be able to control if someone sees a piece of HTML. You could create a custom model to define your permission on, like so:

class MyPermObject(models.Model):
    pass

And then create the said permission like so:

content_type = ContentType.objects.get_for_model(MyPermObject)
permission = Permission.objects.create(codename='can_see_html', name='Can see my HTML', content_type=content_type)

See the django docs to find out how to query the user's permission: https://docs.djangoproject.com/en/1.9/topics/auth/default/#topic-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!