title: Count and Sum objects from different models - Django
body: <p>I'm working on my Django project and I'm triying to order my posts by the sum of 2 related models.</p>
  <p>So this query should take the attendance count from the Post model and the attendant count from the Attending model and get the total sum.</p>
  <p>These are the models:</p>
  <pre><code>class Post(models.Model):
      title = models.CharField(max_length=100)
      attendance = models.ManyToManyField(User, related_name='user_event_attendance')
  </code></pre>
  <pre><code>class Attending(models.Model):
      attendant = models.ForeignKey(User, related_name='events_attending', on_delete=models.CASCADE, null=True)
      post = models.ForeignKey('Post', on_delete=models.CASCADE, null=True)
  </code></pre>
  <p>By now I have the following code but is not working properly:</p>
  <p>views.py</p>
  <pre><code>new_query = Post.objects.filter(
              Q(status='NOT STARTED', post_options='PUBLIC') | Q(status='IN PROGRESS',post_options='PUBLIC')).distinct().annotate(total_attendance=Count('attendance')).annotate(total_attendant=Count(&quot;attending__attendant&quot;)).annotate(total=F(&quot;total_attendant&quot;) + F(&quot;total_attendance&quot;)).order_by('-total')
  </code></pre>