Django Content type table - Auth Permission

前端 未结 1 371
深忆病人
深忆病人 2021-01-16 14: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

1条回答
  •  走了就别回头了
    2021-01-16 14:45

    "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

    0 讨论(0)
提交回复
热议问题