title: How to query database with conditional expression in Django?
body: <p>I have three models: Business, Offers and OfferPlan:
  Business:</p>
  
  <pre><code>class Business(models.Model):
      name_of_business = models.CharField(max_length=255)
  </code></pre>
  
  <p>Offers:</p>
  
  <pre><code>class Offers(models.Model):
      business = models.ForeignKey(Business, related_name="business_offer",
                                   on_delete=models.CASCADE)
      title = models.CharField(max_length=255)
      subtext = models.CharField(max_length=255)
  </code></pre>
  
  <p>OfferPlan:</p>
  
  <pre><code>class OfferPlan(models.Model):
      WEEKDAYS = [
          (1, _("Monday")),
          (2, _("Tuesday")),
          (3, _("Wednesday")),
          (4, _("Thursday")),
          (5, _("Friday")),
          (6, _("Saturday")),
          (7, _("Sunday")),
      ]
      offer = models.ForeignKey(Offers, related_name="business_offer_plan",
                                on_delete=models.CASCADE)
      weekday = models.IntegerField(
          choices=WEEKDAYS,
      )
      from_hour = models.TimeField()
      to_hour = models.TimeField()
  </code></pre>
  
  <p>I have a ListView which search for businesses open based on different params such as city, category etc. I also want to now search by weekday, say which business is open on Monday will be displayed and which are not wont be displayed on that day. Weekday information is stored in OfferPlan and there could be multiple timings for the offers that day in OfferPlan table, but I want to query (filter, exclude) the businesses who has even a single entry on that weekday number.</p>
  
  <p>Here is my ListView:</p>
  
  <pre><code>class SearchListView(ListView):
      template_name = 'search/search.html'
      model = Business
  
      def get_queryset(self):
          # queryset = Business.objects.filter(business_address__city=AppLocations.objects.first().city)
          if 'city' in self.request.GET:
              queryset = Business.objects.filter(business_address__city=self.request.GET.get('city'))
  
          if 'category' in self.request.GET:
              queryset = queryset.filter(category__code=self.request.GET.get('category'))
  
          # if 'date' not in self.request.GET:
          #     queryset = B
          raise
          return queryset
  </code></pre>
  
  <p>How could this be possible? Also looked into <a href="https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/</a> but not able to figure out. </p>
  
  <p>Thanks</p>
  
  <hr>
  
  <h2>Update 1</h2>
  
  <hr>
  
  <p>After researching more in the web, I figured out this is how it could be achieved, but need to know for sure from other Django enthusiasts here that it is right.</p>
  
  <pre><code>queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gt=1)
  </code></pre>
  
  <hr>
  
  <h2>Solution</h2>
  
  <hr>
  
  <p>Jefferson's solution was tagged as right answer as it provided more insights, about which query is fast and what wrong was with my previous update, so here is the proper solution to which we both agreed:</p>
  
  <p><code>queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gte=1)</code></p>