title: Django Many to Many, ordering and serializing using REST
body: <p>I was designing a system which allows users to select a number of objects (say 5) in a pre-configured ordered set of objects (count > 5). Basically, the user has to select top 5 objects in a set of 9. I have to save these entries and find out those objects which have the maximum votes (maximum selection) and a winner based on the most right entries.</p>
  
  <p>My models:</p>
  
  <pre><code>class User(AbstractUser):
      '''Info about user '''
  
  class Contest(models.Model):
      name = models.CharField(max_length=255)
      created = models.DateField(auto_now_add=True)
  
  
  class ObjectsForContest(models.Model):
      name = models.CharField(max_length=255)
      contest = models.ManyToManyField('Contest', through='OrderedObjectsForContest')
  
  class OrderedObjectsForContest(models.Model):
      choice = models.ForeignKey(ObjectsForContest)
      contest = models.ForeignKey(Contest, related_name='ordered_options')
      order = models.PositiveIntegerField()
  
      class Meta:
          ordering = ['order']
  
  class UserEntriesForContest(models.Model):
      user = models.ForeignKey(User, related_name="entries_for_contest")
      contest = models.ForeignKey(Contest, related_name='participating_users')
      objects_selected = models.ManyToManyField(ObjectsForContest, through='OrderedChoicesByUser', null=True, blank=True)
  
      def __unicode__(self):
          return self.user.unique_id
  
  class OrderedChoicesByUser(models.Model):
      choice = models.ForeignKey(ObjectsForContest)
      contest = models.ForeignKey(UserEntriesForContest)
      order = models.PositiveIntegerField()
  
      class Meta:
          ordering = ['order']
  </code></pre>
  
  <p>My serializers are pretty straight forward. 
  Serializer:</p>
  
  <pre><code>class ContestObjectsSerializer(serializers.ModelSerializer):
      class Meta:
          model = ObjectsForContest
          exclude = ("contests", )
  
  
  class ContestObjectsThroughSerializer(serializers.ModelSerializer):
      choice = ContestObjectsSerializer()
  
      class Meta:
          model = OrderedObjectsForContest
          exclude = ("id", )
  
  
  class Contest(serializers.ModelSerializer):
      winner = SimpleUserProfileSerializer()  #this serializes the user and not included here.
      choices = ContestObjectsThroughSerializer(many=True)
  
      class Meta:
          model = Contest
  
  
  class UserEntriesForContestSerializer(serializers.ModelSerializer):
      choices_made = ContestObjectsSerializer(many=True)
  
      class Meta:
          model = UserEntriesForContest
  </code></pre>
  
  <p>How can I find the choice that has been selected by the maximum number of users according to their order?</p>
  
  <p>Ex. 
  Objects according to contest: (Name, order)</p>
  
  <pre><code>(Apple, 1), (Orange, 2), (Grapes, 3), (Mango, 4), (Apricot, 5), (Cherry, 6), (Guava, 7), (Jackfruit, 8), (Lime, 9)
  </code></pre>
  
  <p>Let us assume that there are 5 users whose choices are:</p>
  
  <pre><code>#User 1:
  (Apple, 1), (Lime, 2), (Grapes, 3), (Mango, 4), (Apricot, 5)
  #User 2:
  (Apple, 1), (Orange, 2), (Lime, 3), (Mango, 4), (Apricot, 5)
  #User 3:
  (Apple, 1), (Orange, 2), (Grapes, 3), (Cherry, 4), (Apricot, 5)
  #User 4:
  (Apple, 1), (Orange, 2), (Lime, 3), (Cherry, 4), (Apricot, 5)
  #User 5:
  (Apple, 1), (Lime, 2), (Grapes, 3), (Cherry, 4), (Apricot, 5)
  </code></pre>
  
  <p>The winning list should be:</p>
  
  <pre><code>(Apple, 1), (Orange, 2), (Grapes, 3), (Cherry, 3), (Apricot, 5)
  </code></pre>
  
  <p>I need to know how can I get this list given my models. </p>