我需要在管理员中显示get_followers和get_following的数量。
我的模型称为应用程序,每个应用程序都有许多关注者和关注者。我需要显示关注者和关注者的计数。我尝试在单独的函数中使用.count函数,但我遗漏了一些东西。
这就是我现在所拥有的:
class Application(TimeStampModel):
name = models.CharField(verbose_name='CI Name', max_length=100, unique=True)
relationships = models.ManyToManyField('self', through='Relationship',
symmetrical=False,
related_name='related_to')
def get_following(self):
return ", ".join(str(x) for x in self.get_relationships(RELATIONSHIP_FOLLOWING))
def get_following_count(self):
return ", ".join(str(x) for x in self.get_relationships(RELATIONSHIP_FOLLOWING).count())
def get_followers(self):
return ",".join(str(x) for x in self.get_related_to(RELATIONSHIP_FOLLOWING))
def get_friends(self):
return self.relationships.filter(
to_apps__status=RELATIONSHIP_FOLLOWING,
to_apps__from_application=self,
from_apps__status=RELATIONSHIP_FOLLOWING,
from_apps__to_application=self)
def get_friends_display(self):
return ", ".join(str(x) for x in self.get_friends())
答案 0 :(得分:0)
这就是我所做的。
def get_following_count(self):
count_get_following = self.get_relationships(RELATIONSHIP_FOLLOWING).count()
return count_get_following