Django Content type table - Auth Permission

戏子无情 提交于 2019-12-01 10:24:20

问题


I want to add a permission under auth_permission table. When I insert another permission manually, I need to insert a content_type_id also. This is referenced to content_type table. I dont know what it does. I want to remove set of HTML lines if user doesn;t have that permission. What's the importance of content_type_id?


回答1:


"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



来源:https://stackoverflow.com/questions/20870603/django-content-type-table-auth-permission

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