Django内容类型表 - 认证权限 [英] Django Content type table - Auth Permission

查看:112
本文介绍了Django内容类型表 - 认证权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 auth_permission 表下添加权限。当我手动插入另一个权限时,我还需要插入一个 content_type_id 。这被引用到 content_type 表。我不知道它是做什么的我想删除一组HTML行,如果用户没有;有这个权限。 content_type_id 的重要性是什么?

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?

推荐答案

content_type_id的字段由Django确定。

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

该字段是与ContentType实例的外键关系。

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

这个领域的重要性在于它指定了许可适用的对象(从django源代码1.7.11):

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".

在用例中,您希望能够控制是否有人看到一段HTML,您可以创建一个定制模型来定义你的权限,如下所示:

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)

请参阅django文档,了解如何查询用户权限: https://docs.djangoproject.com/en/1.9/topics/auth/default / #topic-authorization

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

这篇关于Django内容类型表 - 认证权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆