Django多对多的关系,并通过

时间:2022-10-04 19:23:16

I want to store which user invited another user to a group... but django is telling me this is ambigous and against the rules (which makes sense).

我想存储哪个用户邀请了另一个用户到一个群组......但是django告诉我这是一个暧昧而违反规则(这是有道理的)。

groups.group: Intermediary model Group_to_Member has more than one foreign key to User, which is ambiguous and is not permitted.

groups.group:中间模型Group_to_Member具有多个用户的外键,这是不明确的,不允许使用。

So how do I do this correctly? Maybe a generic relation? might work but seems a bit convoluted... Here's how I was approaching it (with unrelated bits removed)

那么我该怎么做呢?也许是一般关系?可能会工作但似乎有点复杂......这就是我接近它的方式(删除了不相关的位)

from django.contrib.auth.models import User

class UserGroup(models.Model):
    members = models.ManyToManyField(User, through='Group_to_Member')

class UserGroup_to_Member(models.Model):
    group = models.ForeignKey(UserGroup)
    member = models.ForeignKey(User)

    invited_by = models.ForeignKey(User, related_name="group_invited_users")

Solution

Ok so I did a little combination of the answers you guys provided (Thanks!) and things I found on the internet plus my own admittedly meager python-fu:

好的,所以我做了一些你们提供的答案的组合(谢谢!)和我在互联网上找到的东西以及我自己公认的微薄的python-fu:

from django.contrib.auth.models import User

class UserGroup(models.Model):
    # notice there is no member object here
    ... other model data

    def add_member(self, **kwargs):
        g2m = UserGroup_to_Member(group = self,  **kwargs)
        g2m.save()

    def remove_member(self, member):
        g2m = UserGroup_to_Member.objects.get(group=self, member=member)
        g2m.delete()

    # This is not elegant at all, help please? I'm pretty sure it isn't
    # as bad on the database as it looks though.
    def get_members(self):
        g2ms = UserGroup_to_Member.objects.filter(group=self)
        member_ids = [g2m.member.id for g2m in g2ms]
        members = User.objects.none()
        for id in member_ids:
            members = members | User.objects.get(id=id)
        return members

class UserGroup_to_Member(models.Model):
    group = models.ForeignKey(UserGroup)
    member = models.ForeignKey(User)

    invited_by = models.ForeignKey(User, related_name="group_invited_users")

2 个解决方案

#1


8  

You have to manage it yourself:

你必须自己管理它:

class MyGroup(models.Model):
    name = models.CharField(max_length=100)

class Membership(models.Model):
    group = models.ForeignKey(MyGroup)
    member = models.ForeignKey(User)

    invited_by = models.ForeignKey(User, related_name='invited_set')

Then instead of group.members.all() you do group.membership_set.all().

然后代替group.members.all()你做group.membership_set.all()。

Also, I wouldn't use 'Group' as your model name, as Django already has a Group object.

另外,我不会使用'Group'作为您的模型名称,因为Django已经有了一个Group对象。

#2


0  

It is possible if you are using Django 1.7.

如果你使用Django 1.7,这是可能的。

From the docs: https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships

来自文档:https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships

In Django 1.6 and earlier, intermediate models containing more than one foreign key to any of the models involved in the many-to-many relationship used to be prohibited.

在Django 1.6及更早版本中,曾经禁止在涉及多对多关系的任何模型中包含多个外键的中间模型。

#1


8  

You have to manage it yourself:

你必须自己管理它:

class MyGroup(models.Model):
    name = models.CharField(max_length=100)

class Membership(models.Model):
    group = models.ForeignKey(MyGroup)
    member = models.ForeignKey(User)

    invited_by = models.ForeignKey(User, related_name='invited_set')

Then instead of group.members.all() you do group.membership_set.all().

然后代替group.members.all()你做group.membership_set.all()。

Also, I wouldn't use 'Group' as your model name, as Django already has a Group object.

另外,我不会使用'Group'作为您的模型名称,因为Django已经有了一个Group对象。

#2


0  

It is possible if you are using Django 1.7.

如果你使用Django 1.7,这是可能的。

From the docs: https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships

来自文档:https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships

In Django 1.6 and earlier, intermediate models containing more than one foreign key to any of the models involved in the many-to-many relationship used to be prohibited.

在Django 1.6及更早版本中,曾经禁止在涉及多对多关系的任何模型中包含多个外键的中间模型。