title: Count and Q/F query with django
body: <p>I want to use Q/F objects with a Count and I get the following error:</p>
  
  <pre><code>int() argument must be a string or a number, not 'Count'
  </code></pre>
  
  <p>I want to get all the deals such that:</p>
  
  <ul>
  <li>the start_date is a date in the past or today</li>
  <li>the end_date is a date in the future or today OR the number of
  participations (DealsParticipation model) is lower than the maximum
  number of participants of the deal (field nb_participants).</li>
  </ul>
  
  <p>My model:</p>
  
  <pre><code>class Deals(models.Model):
      u"""List of deals available to the participant 
      """
  
      brand = models.ForeignKey(Brand_Advertiser, blank=False, null=False, related_name='brands_of_deal')
      start_date = models.DateField(_("Date de debut"), blank=False, null=False, default=datetime.date.today)
      end_date = models.DateField(_("Date de fin"), blank=True, null=True)
      nb_participants=models.PositiveIntegerField(_("Nombre de participants maximum"), default=100, blank=True, null=True)
  
  
  class DealsParticipation(TimeStampedModel):
  
      u"""Participation to deals by participants
      """
  
      deal = models.ForeignKey('Deals', blank=False, null=False, related_name='deal_participation')
      participant = models.ForeignKey(Participant, blank=False, null=False)
  </code></pre>
  
  <p>What I have tried:</p>
  
  <pre><code>class CurrentDealsView(ListView):
      model = models.Deals
  
      def get_queryset(self):
          #get all the deals having started in the past or today and closing in the future or today
          now = datetime.now()
          start_date_condition=Q(start_date__lte=now)
          end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
          #get all the deals with a number of participations lower than the maximal number of participants
          nb_participants_condition=Q(nb_participants__gt=Count('deal_participation'))
          return models.Deals.objects.filter(start_date_condition &amp; (end_date_condition | nb_participants_condition))
  </code></pre>
  
  <h2><strong>Solved!</strong></h2>
  
  <p>As explained by emulbreh, I had to use the count inside annotate:</p>
  
  <pre><code>def get_queryset(self):
      #get all the deals having started in the past or today and closing in the future or today
      now = datetime.now()
      start_date_condition=Q(start_date__lte=now)
      end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
      date_conditions=models.Deals.objects.filter(start_date_condition &amp; end_date_condition)
  
      #get all the deals with a number of participations lower than the maximal number of participants
      return date_conditions.annotate(participation_count=Count('deal_participation')).filter(nb_participants__gt=F('participation_count'))
  </code></pre>