Django:想要一个 ManyRelatedManager,而是获取 ManyToManyField

发布于 2024-11-04 21:16:51 字数 698 浏览 4 评论 0原文

因此,在我的项目中,我尝试将 User 模型扩展到 Staff 类,将 Group 模型扩展到 PermGroup 类。但是,当我在 Staff 的组字段(继承自 User)中保存 PermGroup 时,它仅将 PermGroup 对象保存为 Group,并且我在 PermGroup 类中定义的所有字段和方法都被删除。所以我决定最好的做法是覆盖 groups 字段。从我发现的早期 stackoverflow 问题和 Django 文档来看,这应该可行。

class Staff(User):
    User.groups = models.ManyToManyField('PermGroup', blank=True)

我需要使用“PermGroup”,因为该类稍后显示在文件中,并且 PermGroup 有一个依赖于 Staff 类的字段,因此如果我切换顺序,我只会在 PermGroup 类中遇到相同的问题。

现在我遇到的问题是,组现在是一个 ManyToManyField 对象,其中所有其他“manytomany”字段都是 ManyRelatedManagers。我希望团体成为 ManyRelatedManager 但我不知道如何。

当我使用“PermGroup”模型调用启动它时,是否可以使组成为 ManyRelatedManager?

如果我的方法是错误的,您可以建议在 Staff 类中保存 PermGroups 的替代方法。我将不胜感激。

So in my project I am trying to extend the User model to a Staff class and the Group model to a PermGroup class. However, when I save a PermGroup in the Staff's groups field (inherited from User) it only saves the PermGroup object as a Group and all of the fields and methods I defined in my PermGroup class are stripped away. So I decided the best course of action would be to override the groups field. From an earlier stackoverflow question I found and Django documentation, this should work.

class Staff(User):
    User.groups = models.ManyToManyField('PermGroup', blank=True)

I need to use 'PermGroup' because the class shows up later in the file, and PermGroup has a field that relies on the Staff class, so if i switched the order I would have the same problem, only in the PermGroup class.

Now the problem I am having is that groups is now a ManyToManyField object where all the other "manytomany" fields are ManyRelatedManagers. I want groups to be a ManyRelatedManager but I do not know how.

Is it possible to get groups to be a ManyRelatedManager when I initiatize it using the 'PermGroup' model call?

If my approach is wrong and you can suggest an alternative to saving PermGroups in the Staff class. I would greatly appreciate it.

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

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

发布评论

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

评论(1

另类 2024-11-11 21:16:51

为什么不让您的 Staff 成为一个标准模型,并为他/她相应的 User 提供一个外键(更准确地说,OneToOneField)?

而且,要消除循环依赖问题,您只需使一个依赖于另一个即可。例如,PermGroup 模型可能有一个包含该组中的多对多员工的字段。 Staff 不需要拥有 PermGroup,因为如果您想查看成员属于哪些组,只需执行以下操作:

groups_theyre_in = PermGroups.objects.filter(staff_members__id=id_were_looking_for)

Why not just have your Staff be a standard model with a ForeignKey (OneToOneField, to be more exact) to his/her corresponding User?

And, to remove the circular dependency problem, you just need to make one dependent on the other. For instance, the PermGroup model could have a field of a ManytoMany of the Staff members in that group. There's no need for Staff to have a PermGroup, because if you wanted to see what groups a member belongs to, you'd just do something like this:

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