问题
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