问题标签 [django-guardian]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
mysql - Guardian 和自定义用户模型:迁移时的 IntegrityError,创建匿名用户
我有这个模型文件,其中定义了自定义用户。
而这个信号
运行第一次迁移时,Django 将我的应用程序很好地迁移到数据库中。但是当我第二次运行迁移时,Django 会抛出这个错误:
django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'email'")。
我正在使用 MySQL。这里出了什么问题?
更新:当我进行第二次迁移时,我发现了引发错误的代码。
我的模型文件中有这段代码:
我将此功能导入到我的设置中,这样当我第一次进行迁移时,将创建匿名用户。并且它确实按预期进行。但是当我再次运行迁移时,django 尝试创建另一个匿名用户,这就是我收到此错误的原因。
当我在我的设置中注释掉这一行时:
" GUARDIAN_GET_INIT_ANONYMOUS_USER = 'latiro_app.models.get_anonymous_user_instance' "
我没有看到这个错误。
python - Django 信号不适用于工作人员
我有使用 websocket 实现的 django (1.9) 应用程序。为了实现 websocket,我使用了 Django 通道(1.1.8)。我的模型在 ACL 下,由 Django-guardian (1.4.4) 管理。
每次创建、更改或删除名为“My_model”的模型时,我都会通过 websocket 发送消息。但我的挑战是模型在 ACL 下,这意味着当且仅当该用户可以查看模型实例时,用户才能通过 websocket 接收消息。这意味着我必须在我的信号处理程序中有用户实例,以便在发送消息之前验证 websocket 用户是否具有 perm。
你可以在下面看到我的实现。它适用于开发环境,但不适用于生产环境。不同之处在于,在生产环境中,websocket 处理程序在 Django 工作人员和 Daphne 服务器下运行。
在文件 my_app/consumers/my_model_consumer.py 中:
有人知道是什么问题导致它无法在生产环境中工作吗?
python - Complex Django permissions
I'm working with the following models in a django project. The effective relationship is that a District can have multiple Schools, a School can have multiple Students, and a Student may have multiple Cases.
A user may be assigned:
- a specific case
- a specific student (which would include all cases for that student)
- a specific school (which would include all students for that school, and thus all cases for the students at that school)
- a specific district (which would include all schools for that district, all students that attend those schools within that district, and all cases for students that attend those schools within that district)
Users (other than Admins) will only be able to view/edit/delete models for which they have been granted permissions. Admins will have the former permissions for all models as well as adding new models. Admins will ultimately "assign" who has access to what, groups seemed like a natural way to extend some of this functionality.
I'm aware that django provides no native support for instance-level permissions and as such I've already explored the django-guardian package. It appears to offer the functionality required for a specific instance and while I can create permissions individually for objects through the shell, I'm obviously wanting to build this functionality into the models.
Ideally when a model is created, a group (or permission?) will be available that users can be placed into such as
I think programmatically creating Groups for each object permission would be ideal? But then I would then need to remove those groups if the model is deleted? I also feel like groups may be overcomplicating the system and rather rely on programmatically generated permissions and avoiding groups outright? I really am looking for a some direction as I've never needed to create such a unique permission scheme. Any and all advice is greatly appreciated.
python - 克隆 django-guardian 中的所有对象权限/从 ForeignKey 继承
我有一个具有模型层次结构的应用程序,其中我需要底层对象具有与父对象相同的权限(不仅是它们的定义/代号,还有每个用户和每个组的权限)。
Django-guardian 似乎只有允许检查特定用户/组权限的功能。
是否有任何规范的方法可以将所有权限从一个对象克隆到另一个对象或强制继承?
django - Django中基于行的权限
我有一个简单的 Django 模型,大致如下所示:
数据库表中将有几百万行。
django 管理界面的基于行的权限应该得到实现。
如果当前用户在“responsible_group”中,则允许他查看和修改它。
AFAIK django Guardian 不太适合这个。请参阅此相关页面:https ://django-guardian.readthedocs.io/en/stable/userguide/performance.html
甚至解决方案“直接外键”也不匹配。当前模型已经包含过滤行所需的所有内容。
如何为 django 启用 row-based-permission 并使用 Log 模型进行权限检查?
python - 在 Django 中为每个对象定义和分配角色
使用django-guardian定义角色的好方法是什么?我想为用户设置对象的权限组,以便可以将权限添加到角色并可供与特定对象关联的所有用户访问。
想象一下,您正在构建一个项目管理应用程序。用户可以是项目经理或开发人员。一个用户可以参与多个项目,在一些项目中担任开发人员的角色,在另一些项目中担任项目经理。
这个想法是定义那些角色,这些角色将允许将权限分配给一个对象,在这种情况下是一个项目,“批量”给用户,并且如果一个新的权限被添加到一个特定的角色,这个角色将被更新并且用户将拥有对分配了该角色的所有对象的新权限。
我发现做这样的事情的项目是django-permissions,它有一个叫做 Local Role 的东西,这基本上是我想用 django-guardian 实现的。遗憾的是,该软件包的文档记录非常差,并且在过去 4 年中没有更新。
python - 避免在 Django 中获取用户和权限的 N+1 问题
我需要获取每个用户拥有的用户列表和权限列表(监护人对象权限)。问题是我试图这样做的每一种方式都会遇到 N+1 问题。我是否只是简单地遍历用户列表并从集合中获取权限
或者如果我使用 prefetch_related()
它最终首先获取用户列表,然后为每个用户运行单独的权限查询。
我可以编写一个原始 SQL 语句并自己序列化它,但我宁愿使用我已经拥有的序列化程序,因此我希望将用户和权限作为模型实例。有什么方法可以使用 ORM 完成我所需要的吗?
Django 是 1.11 LTS 版本,我不能很快升级到 2.x。
python - django 中的字段级权限
在 django 中,我可以使用 django-guardian 来获得用户和组的对象级权限。但是如果我想设置字段级别的权限呢?
假设我有一个模型如下:
我的系统中有三个用户,它们是user1、user2和user3
现在我希望user1将获得模型Attribute的所有三个字段的权限。但是user2将仅获得name和geg的权限,而user3将仅获得name字段的权限。
在我的系统中有很多用户和组。有没有像 django-guardian 这样的方法在 django.xml 中设置字段级权限。
django - 使用 django-guardian 进行 djangorestframework 的权限不起作用
我正在尝试使用 django-guardian 向我的 django REST 项目添加对象级别权限,但我得到了
http://127.0.0.1:8000/api/v1/tasks/
HTTP 403 禁止 允许:GET、POST、HEAD、OPTIONS 内容类型:application/json 变化:接受
{ "detail": "您无权执行此操作。" }
用户 joe 已登录。
设置.py:
模型.py:
序列化程序.py:
权限.py:
过滤器.py:
视图.py:
网址.py:
但它在shell中工作正常