Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Django:如何添加不相关对象的计数?_Sql_Django_Count_Relation - Fatal编程技术网

Sql Django:如何添加不相关对象的计数?

Sql Django:如何添加不相关对象的计数?,sql,django,count,relation,Sql,Django,Count,Relation,我有两个间接相关的表-Posts和Follower\u to\u followee models.py: class Post(models.Model): auth_user = models.ForeignKey(User, null=True, blank=True, verbose_name='Author', help_text="Author") title = models.CharField(blank=True, max_length=255, help_t

我有两个间接相关的表-
Posts
Follower\u to\u followee

models.py:

class Post(models.Model):

    auth_user = models.ForeignKey(User, null=True, blank=True, verbose_name='Author', help_text="Author")

    title = models.CharField(blank=True, max_length=255, help_text="Post Title")

    post_content = models.TextField (help_text="Post Content")

class Follower_to_followee(models.Model):

    follower = models.ForeignKey(User, related_name='user_followers', null=True, blank=True, help_text="Follower")

    followee = models.ForeignKey(User, related_name='user_followees', null=True, blank=True, help_text="Followee")
folowee与post中的post auth_用户(post author)间接相关。不过,它与Django用户表直接相关,而用户表与post表直接相关


如何在不涉及用户表的情况下为特定跟随者选择所有跟随者,并在查询结果中包含每个跟随者的post计数?实际上,在这一点上,我甚至不清楚如何在涉及用户表的情况下做到这一点。请帮忙。

我不能完全确定我是否理解这个问题,但这里有一个简单的解决方案。请注意,这可以写得更简洁,但我将其分解,以便您可以看到每个步骤

如何为特定跟随者选择所有跟随者


对于get post计数,您可以使用以下方法:

#get follower
follower = User.objects.get(username='username_of_fallower')
#get all followees for a specific follower
for element in Follower_to_followee.objects.filter(follower=follower):
    element.followee.post_set.all().count()

视图.py

def view_name(request):
    followers = Follower_to_followee.objects.filter(user=request.user)
    .......
html

{{user}}<br/>

My followers:<br/>    
{% follower in followers %}
    <p>{{follower}} - {{follower.user.follower_to_followee_set.count}}</p>
{% endfor %}
{{user}}
我的追随者:
{followers%中的%follower%} {{follower}}-{follower.user.follower_to_followere_set.count}

{%endfor%}
可以编写生成单个SQL的查询,请尝试以下操作

qs = User.objects.filter(user_followees__follower=specific_follower).annotate(
         post_count=models.Count('post'))
for u in qs:
    print u, u.post_count
检查第二部分(除额外的M2M管理器外,工作方式类似)

User.objects.filter
内部使用时,
User\u followees\u follower=foo
User\u followee\u followee=foo
都会导致
follower\u到followee
模型和
其中的
条件检查
follower=foo
followee=foo

(请注意,
user\u followees\u followee=foo
user\u followeers\u followers=foo
的工作方式与上述不同,Django ORM巧妙地简化了它们,并将生成类似于
user.objects.filter(pk=foo.pk)
)的内容。

非常感谢大家!我使用了生成单个SQL的查询,它工作得非常好!现在,我可以显示特定跟随者的跟随者列表,并在跟随者名称旁边输出每个跟随者的post计数。。。
qs = User.objects.filter(user_followees__follower=specific_follower).annotate(
         post_count=models.Count('post'))
for u in qs:
    print u, u.post_count