title: Exclude one row of duplicate rows in Django
body: <p>I have a model like this (simplified):</p>
  
  <pre><code>class TrainingMoment(models.Model):
      date = models.DateField()
      # Moment of the day, 2 is afternoon for example
      moment_nr = models.IntegerField()
      is_group_moment = models.BooleanField()
  </code></pre>
  
  <p>On a given date and moment_nr, 2 rows can exist. One with <code>is_group_moment=False</code> and one with <code>is_group_moment=True</code>.
  For every one of the duplicates in <code>TrainingMoment.objects.filter(date__range=(start_date,end_date))</code> i want to exclude the row with <code>is_group_moment = True</code>.</p>
  
  <p>Note that I only want to exclude the rows with <code>is_group_moment=True</code> if there is a row with the same date and moment_nr with <code>is_group_moment=False</code>.</p>
  
  <p>I have tried using annotate() together with group_by() to get the duplicate rows to begin with, but that gives med only one of each duplicate row set, instead of both as I want.</p>
  
  <p>For example:</p>
  
  <pre><code>╔════════════════════════════════════════╗
  ║ date       moment_nr is_group_moment   ║
  ╠════════════════════════════════════════╣
  ║ 2013-10-01 1         True              ║
  ║ 2013-10-02 1         True              ║
  ║ 2013-10-02 1         False             ║
  ║ 2013-10-03 1         False             ║
  ║ 2013-10-03 2         False             ║
  ║ 2013-10-04 2         True              ║
  ║ 2013-10-04 2         False             ║
  ║ 2013-10-01 1         True              ║
  ╚════════════════════════════════════════╝
  </code></pre>
  
  <p>Should be:</p>
  
  <pre><code>╔═════════════════════════════════════════╗
  ║ date       moment_nr is_group_moment    ║
  ╠═════════════════════════════════════════╣
  ║ 2013-10-01 1          True              ║
  ║ 2013-10-02 1          False             ║
  ║ 2013-10-03 1          False             ║
  ║ 2013-10-03 2          False             ║
  ║ 2013-10-04 2          False             ║
  ║ 2013-10-01 1          True              ║
  ╚═════════════════════════════════════════╝
  </code></pre>
  
  <p>I have another model, Activity, which stores training time where I want to use the queryset from above to get correct summing of the week:</p>
  
  <pre><code>class Activity(models.Model):
      activitytype = models.ForeignKey(ActivityType)
      trainingmoment = models.ForeignKey(TrainingMoment)
      # Time in minutes
      time = models.IntegerField()
  </code></pre>
  
  <p>Summary of what i want to accomplish:</p>
  
  <pre><code># 1. Get training moments for a given period, for example a week (no problem here)
  tms_for_summing = TrainingMoment.objects.filter(date__range=(start_date,end_date))
  
  # 2. Filter out duplicates in the way described above
  
  # 3. Use the resulting queryset (tms_for_summing) to sum activity
  summed_activity = Activity.objects.filter(trainingmoment__in = tms_for_summing)    
  </code></pre>
  
  <p><strong>Edit</strong>
  Here follows some additional explanation for those who wonder about my db design:</p>
  
  <p>This is as you have probably figured out, a training logging app. In the case in my question, the page I want to achieve is a planning page. All the training moments are planned training moments. An individual athlete can plan his own training. Also, a trainer can plan training for a group of athletes at the same time. This becomes a group moment (a training moment with is_groupmoment = True and also a many to many field linking to the specific group). If the athlete plans a group moment on a moment nr and date where he also has a group moment, his own moment should override this moment. </p>
  
  <p><strong>A practical, very simplified example</strong></p>
  
  <p>I have the following individual moments:
  Day, Momentnr
  Monday 1
  Tuesday 1</p>
  
  <p>And the following group moment:
  Day, Momentnr
  Tuesday 1
  Wednesday 1</p>
  
  <p>In the table where I show the moments and sum activity connected to these moments, I want to show the individual moments from monday and tuesday, but also the group moment from wednesday since there is no individual moment overriding the group moment there.
  I could do the summing in the view using pure python code by summing the moments one by one, while checking if there are both group moments and individual moment at the same date an momentnr, but this would be a really ugly and slow way to do it, specially when summing a whole year.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>I have tried this:</p>
  
  <pre><code>training_moments = TrainingMoment.objects.filter(date__range=('2013-08-19','2013-10-28'))
  moments_to_exclude = training_moments.annotate(num_dates=Count('date'), num_momentnrs=Count('momentnr')).filter(num_dates__gt= 1, num_momentnrs__gt=1)
  </code></pre>
  
  <p>This is close. With this I get one moment for every "time slot" (where moment nr and date is the same for group moment and individual moment). The problem is that I need to get both the "colliding" moments. Then I can exclude the group moments from the resulting query set and finally have the moments to exclude when summing training moments in my app.</p>
  
  <pre><code>moments_to_exclude = moments_to_exclude.exclude(is_group_moment=True)
  desired_result = training_moments.exclude(pk__in=moments_to_exclude)
  </code></pre>