简体   繁体   English

Django相关model字段查询(互交朋友)

[英]Django related model field querying (mutual friends)

I have a friendship model:我有一个友谊model:


class Friendship(models.Model):
    user = models.ForeignKey(
        Account, on_delete=models.CASCADE, related_name="friend1", null=True, blank=True)
    other_user = models.ForeignKey(
        Account, on_delete=models.CASCADE, related_name="friend2", null=True, blank=True)
    date_created = models.DateTimeField(auto_now=True)

    objects = FriendshipManager()

    class Meta:
        verbose_name = "friendship"
        verbose_name_plural = "friendships"
        unique_together = ("user", "other_user")

    def __str__(self):
        return f'{self.user} is friends with {self.other_user}.'

and this function to return all users who are mutual friends of two accounts和这个 function 返回两个帐户的共同朋友的所有用户


def mutual_friends(self, account1, account2):
        mutual_friends = Account.objects.filter(
            Q(friend2__user=account1) & Q(friend2__user=account2))
        return mutual_friends

Based on my (limited) understanding of how the query api works, I would think this should return all users who have a "friend2" relationship with the Friendship table where the "friend1" user is either account1 or account2.根据我对查询 api 工作原理的(有限)理解,我认为这应该返回与 Friendship 表具有“friend2”关系的所有用户,其中“friend1”用户是 account1 或 account2。 I'm still getting used to querying with django, so if someone can let me know what I'm doing wrong that'd be great.我仍然习惯于使用 django 进行查询,所以如果有人能让我知道我做错了什么,那就太好了。

Thanks!谢谢!

Your model design doesn't seem right to me.您的 model 设计对我来说似乎不合适。 As of now, you can put any Account instance as user or other_user , and as they both refer the same model ( Account ), while doing any retrieval from database, you need to account for both of the fields.到目前为止,您可以将任何Account实例作为userother_user ,因为它们都引用相同的 model ( Account ),在从数据库中进行任何检索时,您需要考虑这两个字段。

A better design IMO would be to use a ManyToManyField (many-to-many relationship) in the Account model to itself, as one account can have multiple other accounts as friends and vice-versa.更好的设计 IMO 是在Account model 中使用ManyToManyField (多对多关系),因为一个账户可以有多个其他账户作为朋友,反之亦然。 So:所以:

class Account(models.Model):
    ...
    friends = models.ManyToManyField('self')
    ...

Now, you can add friends like eg:现在,您可以添加朋友,例如:

account_foo.friends.add(account_bar, account_spam)

account_* are Account instances. account_*Account实例。

You can get all friends of account_foo like:您可以获得account_foo的所有朋友,例如:

account_foo.friends.all()

Check out the many-to-many doc for various examples of data integration and querying.查看多对多文档,了解数据集成和查询的各种示例。


Now, to find the mutual friends of eg account_foo and account_bar , you can first get all the friends of account_foo and then see which of them are also friends with account_bar :现在,要查找例如account_fooaccount_bar的共同好友,您可以先获取account_foo的所有好友,然后查看其中哪些也是account_bar的好友:

friends_with_foo = account_foo.friends.values_list('pk', flat=True)
mutual_friends_of_foo_bar = account_bar.friends.filter(pk__in=friends_with_foo)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM