繁体   English   中英

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

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

我有一个友谊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}.'

和这个 function 返回两个帐户的共同朋友的所有用户


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

根据我对查询 api 工作原理的(有限)理解,我认为这应该返回与 Friendship 表具有“friend2”关系的所有用户,其中“friend1”用户是 account1 或 account2。 我仍然习惯于使用 django 进行查询,所以如果有人能让我知道我做错了什么,那就太好了。

谢谢!

您的 model 设计对我来说似乎不合适。 到目前为止,您可以将任何Account实例作为userother_user ,因为它们都引用相同的 model ( Account ),在从数据库中进行任何检索时,您需要考虑这两个字段。

更好的设计 IMO 是在Account model 中使用ManyToManyField (多对多关系),因为一个账户可以有多个其他账户作为朋友,反之亦然。 所以:

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

现在,您可以添加朋友,例如:

account_foo.friends.add(account_bar, account_spam)

account_*Account实例。

您可以获得account_foo的所有朋友,例如:

account_foo.friends.all()

查看多对多文档,了解数据集成和查询的各种示例。


现在,要查找例如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