Django 用户“每个项目”小组分配

发布于 2024-09-04 09:42:53 字数 342 浏览 11 评论 0原文

这是我的问题:我的网站有用户,他们可以创建项目并访问其他用户的项目。 每个项目可以为用户分配不同的权限。

因此,我可以拥有项目 A:用户“John”位于组“manager”中,项目“B”用户“John”位于组“worker”中。

我如何使用 Django 用户身份验证模型来做到这一点?

从 SQL 的角度来看,我希望能够在“auth_user_groups”表的主键中添加“project_id”。

我认为个人资料在这里没有任何帮助。有什么建议吗?

更新:“工人”和“经理”只是我的应用程序定义的权限组(或“角色”)的两个示例。未来还会有更多。例如:我可能还会有“管理”、“报告”等...

Here's my problem : my site has users, which can create projects, and access other user's projects.
Each project can assign different rights to users.

So, i could have Project A : user "John" is in group "manager" , and Project "B" user "John" is in group "worker".

How could I use the Django User authentication model to do that ?

From a SQL point a view, what i would like is to be able to add "project_id" in the primary key for the "auth_user_groups" table.

I don't think profile is of any help here. Any advice ?

UPDATE : "worker" and "manager" are just two examples of the permission group (or "roles") that my application defines. There will be more in the future. Eg : i will probably also have "admin", "reporting", etc...

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

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

发布评论

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

评论(2

圈圈圆圆圈圈 2024-09-11 09:42:53

如果您确实在组中需要这些信息,我认为最好的解决方案是对必要的模型进行子类化,并从中创建您自己的身份验证应用程序/身份验证后端!

我不知道是否有必要将项目与组相关联,您还可以创建一个与用户具有 1:1 关系且与项目具有 1:1 关系的用户配置文件,因此您可以将项目分配给用户,然后签入您的应用程序 用户与哪些项目相关?

我想到的第三个解决方案是拥有一个模型,其中一个外键用于项目,一个外键用于分组,这可能也适合您,但可能没有很好的可用性!

最肮脏的方法可能是使用猴子修补(add_to_class)将外键添加到组中,但我想只有当您找不到另一种下降方法来做到这一点时才推荐这样做,并且在某种程度上是您的最后手段,但不是首选!

If you really need this information in the groups i think the best solution is to sub class the necessary models and make your own auth application/auth backend out of this!

I dont know if it's necessary to relate the projects to groups, you could also create an user profile that has a 1:1 relation to the user and a m:n to projects, so you could assign projects to a user and then check in your apps what projects the user is related to?

Third solution that comes to my mind would be having a model with one foreignkey to project and one to group, which might also work for you, but probably has no good usability!

The dirtiest way would probably be to add a foreignkey to group using monkey-patching (add_to_class), but i guess this only recommendable if you don't find another descent way to do it and is somehow your last resort, but not first choice!

盗琴音 2024-09-11 09:42:53

如果您可以在用户模型中没有它并且可以拥有关联的用户配置文件模型,那么像这样的东西怎么样:

from django.contrib.auth.models import User
from foo.models import Project

class UserProfile(models.Model):

  auth_user = models.ForeignKey('User')
  projects_where_manager   = models.ManyToManyField('Project', 
                                                     related_name="managers_for_project")
  projects_where_worker   = models.ManyToManyField('Project', 
                                                     related_name="workers_for_project")

If you can live without it in your User model and can have it an associated user profile model, how about something like:

from django.contrib.auth.models import User
from foo.models import Project

class UserProfile(models.Model):

  auth_user = models.ForeignKey('User')
  projects_where_manager   = models.ManyToManyField('Project', 
                                                     related_name="managers_for_project")
  projects_where_worker   = models.ManyToManyField('Project', 
                                                     related_name="workers_for_project")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文