title: optimizing no. of queries being fired in django orm for a given model
body: <p>I have an object, that is uploaded by the user, it contains several details, but for the sake of clarity, can be simply defined by the following model representation - </p>
  
  <p>After this other users can upvote and downvote what this user has uploaded hence a <strong>vote model</strong>.</p>
  
  <p>Now I want to get the upvotes and downvotes of all the objects to be displayed in the template. Hence I add two functions to the <code>ObjectDetail</code> class, as upvote and downvote.</p>
  
  <p>The trouble with this model is, say there are 20 objects, for each object 2 queries are fired one to get the upvote and the other to get the downvote. Hence the no. of queries are 40 now for 20 objects. </p>
  
  <p>What would be a good way to tweak this to reduce the number of queries, and display the upvotes and downvotes on each of the object?</p>
  
  <pre><code>class ObjectDetail(models.Model):
      title = models.CharField()
      img = models.ImageField()
      description = models.TextField()
      uploaded_by = models.ForeignKey(User, related_name='voted_by')
  
      @property
      def upvote(self):
          upvote = Vote.objects.filter(shared_object__id = self.id, 
                               vote_type = True).count()
          return upvote
  
      @property
      def downvote(self):
          downvote = Vote.objects.filter(shared_object__id = self.id, 
                                 vote_type = False).count()
          return downvote
  
  class Vote(models.Model):
      vote_type = models.BooleanField(default = False)
      voted_by =  models.ForeignKey(User, related_name='voted_by')
      voted_for = models.ForeignKey(User, related_name='voted_for')
      shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True)
      dtobject  = models.DateTimeField(auto_now_add=True)
  </code></pre>