这个针对 django 中每个对象权限的 hack 有效吗?

发布于 2024-08-24 19:25:43 字数 642 浏览 2 评论 0原文

根据文档,一个类可以具有元选项权限,描述如下:

Options.permissions

创建此对象时进入权限表的额外权限。为每个具有管理员设置的对象自动创建添加、删除和更改权限。此示例指定额外权限 can_deliver_pizzas:

permissions = (("can_deliver_pizzas", "Can deliver pizzas"),)

这是一个列表或二元组的元组,格式为(permission_code, human_read_permission_name)。

是否可以通过以下方式在运行时定义权限:

permissions = (("can_access_%s" % self.pk, /
                "Has access to object %s of type %s" % (self.pk,self.__name__)),)

According to the documentation, a class can have the meta option permissions, described as such:

Options.permissions

Extra permissions to enter into the permissions table when creating this object. Add, delete and change permissions are automatically created for each object that has admin set. This example specifies an extra permission, can_deliver_pizzas:

permissions = (("can_deliver_pizzas", "Can deliver pizzas"),)

This is a list or tuple of 2-tuples in the format (permission_code, human_readable_permission_name).

Would it be possible to define permissions at run time by:

permissions = (("can_access_%s" % self.pk, /
                "Has access to object %s of type %s" % (self.pk,self.__name__)),)

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-08-31 19:25:43

我认为在 Meta 类的上下文中,您无权访问 self
如果您正在寻找管理应用程序的解决方案,请阅读有关行级别权限的内容。

也有这样的说法:

对于面向公众(即非管理员)的视图,您当然可以自由地实现应用程序所需的任何形式的权限检查逻辑。

I think in the context of the Meta class, you don't have access to self.
If you look for a solution for the admin application, read this about row level permissions.

There is also says:

For public-facing (i.e., non-admin) views, you are of course free to implement whatever form of permission-checking logic your application requires.

空城旧梦 2024-08-31 19:25:43

不,由于多种原因,这行不通。首先,正如 Felix 指出的那样,此时您无法访问 self 。其次,正如您引用的文档所述,这是要输入权限表的项目列表 - 换句话说,这些是实际的数据库行,由 manage.pysyncdb< 创建/代码>。

No, this wouldn't work, for a number of reasons. Firstly, as Felix points out, you have no access to self at that point. Secondly, as the documentation you quoted states, this is a list of items to enter into the permissions table - in other words these are actual database rows, which are created by manage.py syncdb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文