亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查 django 模型 ManyToManyField 是否對稱,如果對稱 = False?

如何檢查 django 模型 ManyToManyField 是否對稱,如果對稱 = False?

倚天杖 2022-07-19 15:28:55
我正在嘗試在 Django 中創建一個追隨者系統。follows = models.ManyToManyField('self', related_name='follower', symmetrical=False, null=True, blank=True)由于系統不應該是對稱的,我如何檢查兩個用戶是否互相關注?如果可能的話,我想在模型中有一個函數。我可能會在limit_choices_to另一個領域和其他一些地方使用這個功能。我最初的想法是添加一個“朋友”字段并使其對稱,但這可能會在以后帶來其他問題。
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我沒有找到一個簡單的解決方案,不需要額外的模型。這是我所做的:


Person(models.Model)如果您有類似的CustomUser(AbstractUser)實現,則不需要創建。否則Person,類在許多情況下都很有用。


count_函數非常有用,但對于大多數用例來說不是必需的。


在里面accounts/models.py我補充說:


RELATIONSHIP_FOLLOWING = 1

RELATIONSHIP_BLOCKED = 2

RELATIONSHIP_STATUSES = (

    (RELATIONSHIP_FOLLOWING, 'Following'),

    (RELATIONSHIP_BLOCKED, 'Blocked'),

)


class Person(models.Model):

     relationships = models.ManyToManyField('self', through='Relationship',

                                           symmetrical=False,

                                           related_name='related_to')


     def get_relationships(self, status):

        return self.relationships.filter(

            to_people__status=status,

            to_people__from_person=self)


    def get_related_to(self, status):

        return self.related_to.filter(

            from_people__status=status,

            from_people__to_person=self)


    def get_following(self):

        return self.get_relationships(RELATIONSHIP_FOLLOWING)


    def get_followers(self):

        return self.get_related_to(RELATIONSHIP_FOLLOWING)


    def count_following(self):

        return len(self.get_relationships(RELATIONSHIP_FOLLOWING))


    def count_followers(self):

        return len(self.get_related_to(RELATIONSHIP_FOLLOWING))


    def get_friends(self):

        return self.relationships.filter(

            to_people__status=RELATIONSHIP_FOLLOWING,

            to_people__from_person=self,

            from_people__status=RELATIONSHIP_FOLLOWING,

            from_people__to_person=self)


    def count_friends(self):

        return len(self.get_friends())


class Relationship(models.Model):

    from_person = models.ForeignKey(Person, related_name='from_people', on_delete=models.CASCADE)

    to_person = models.ForeignKey(Person, related_name='to_people', on_delete=models.CASCADE)

    status = models.IntegerField(choices=RELATIONSHIP_STATUSES)


    def add_relationship(self, person, status):

        relationship, created = Relationship.objects.get_or_create(

            from_person=self,

            to_person=person,

            status=status)

        return relationship


    def remove_relationship(self, person, status):

        Relationship.objects.filter(

            from_person=self,

            to_person=person,

            status=status).delete()

        return


查看完整回答
反對 回復 2022-07-19
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號