title: How to Sum a value with row duplicates in the Django orm query
body: <h2>Goal</h2>
  
  <p>I am looking for a way to get a sum of values</p>
  
  <hr>
  
  <h2>Models</h2>
  
  <pre><code>class ModelX(models.Model):
      name = models.CharField(...)
  
  
  class ModelA(models.Model):
      value = models.IntegerField()
  
  class ModelB(models.Model):
      modela = models.ForeignKey(ModelA)
      modelxs = models.ManyToManyField(ModelX, through='ModelC')
  
  class ModelC(models.Model):
      modelb = models.ForeignKey(ModelB)
      modelx = models.ForeignKey(ModelX)
      modelxx = models.ForeignKey(ModelX, related_name='modelcs')
  </code></pre>
  
  <h2>What I've tried</h2>
  
  <pre><code>Modelx.objects.values('name').annotate(
      total=Sum('modelcs__modelb__modela__value')
  ).values('total')
  </code></pre>
  
  <p>And</p>
  
  <pre><code>sum_qs = (
      ModelA
      .objects
      .filter(modelb_set__modelc_set__modelxx=OuterRef('id'))
      .values('modelb_set__modelc_set__modelxx_id')
      .order_by()
      .distinct()
      .values('value')
  )
  
  qs = ModelX.objects.annotate(
      total=Subquery(sum_qs.values('value')))
  </code></pre>
  
  <h2>The Problem</h2>
  
  <ol>
  <li>ModelC can contain multiple rows with the same <strong>modelxx</strong> relation.  This will cause duplicates, and cause the sum of 'value' to get duplicated</li>
  </ol>
  
  <h2>Example</h2>
  
  <p>Below are the example models where I would expect to see the 10 as stated in the expected results up top: <strong>edit</strong></p>
  
  <pre><code>foo = ModelX.objects.create(name='Foo')
  bar = ModelX.objects.create(name='Bar')
  baz = ModelX.objects.create(name='Baz')
  
  modela1 = ModelA.objects.create(value=10)
  modela2 = ModelA.objects.create(value=10)
  
  modelb1 = ModelB.objects.create(modela=modela1)
  modelb2 = ModelB.objects.create(modela=modela2)
  
  modelc1 = ModelC.objects.create(modelb=modelb1, modelx=foo, modelxx=bar)
  modelc2 = ModelC.objects.create(modelb=modelb1, modelx=bar, modelxx=bar) # This would be a cause a duplicate as a relation to ModelX through modelxx
  modelc3 = ModelC.objects.create(modelb=modelb1, modelx=baz, modelxx=baz)
  modelc4 = ModelC.objects.create(modelb=modelb2, modelx=bar, modelxx=bar)
  </code></pre>
  
  <h2>Expected Results</h2>
  
  <p><strong>EDIT</strong> {'Bar': 20, 'Baz': 10}</p>
  
  <p>This can also be as a QuerySet of ModelX to serialize</p>
  
  <hr>