title: Django - Get distinct data from field
body: <p>I'm looking for getting distinct objects from my database with <code>distinct()</code> command.</p>
  
  <p>I have this model :</p>
  
  <pre><code>class CoefficientIntervention(models.Model):
  
      Technicien = models.FloatField(verbose_name="Coefficient Technicien")
      Ingenieur = models.FloatField(verbose_name="Coefficient Ingénieur")
      Consultant = models.FloatField(verbose_name="Coefficient Consultant")
      Architecte = models.FloatField(verbose_name="Coefficient Architecte")
      Societe = models.ForeignKey(Societe, related_name="ste", verbose_name="Société", default="")
  
      def __unicode__(self):
          return unicode (self.id, self.Technicien, self.Ingenieur, self.Consultant, self.Architecte, self.Societe)
  </code></pre>
  
  <p>My form looks like :</p>
  
  <pre><code>class CoefficientFormulaire(forms.ModelForm) :
  
      Societe = CustomLabelModelChoiceField(Societe.objects.filter(), required=True, label = "Societe", label_func=lambda obj: '%s %s' % (obj.id, obj.Nom))
  
      class Meta :
          model = CoefficientIntervention
          fields = ['Technicien', 'Ingenieur', 'Architecte', 'Consultant','Societe']
  </code></pre>
  
  <p>And my view is really simple :</p>
  
  <pre><code>@login_required
  def Identity_Societe_Coefficient(request) :
  
      if not request.user.is_superuser :
          raise Http404
  
  
      coefficient = CoefficientIntervention.objects.order_by('Societe').values('Societe').distinct()
      print coefficient
  
      if request.method == 'POST':
  
          form = CoefficientFormulaire(request.POST or None)
  
          if form.is_valid() :   # Vérification sur la validité des données
              post = form.save()
  
              messages.success(request, 'Le formulaire a été enregistré !')
              return HttpResponseRedirect(reverse('Coefficient'))
  
          else:
              messages.error(request, "Le formulaire est invalide !")
  
      else:
          form = CoefficientFormulaire()
  
      context = {
          "coefficient" : coefficient,
          "form" : form,
      }
  
      return render(request, 'Identity_Societe_Coefficient.html', context)
  </code></pre>
  
  <p>My table has 3 lines for example :</p>
  
  <p><a href="https://i.stack.imgur.com/PDwl8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PDwl8.png" alt="enter image description here"></a></p>
  
  <p>If I print <code>coefficient</code>, I get : <code>&lt;QuerySet [{'Societe': 2L}, {'Societe': 3L}]&gt;</code></p>
  
  <p>But I would like to display distinct objects in my array and take the last one each time.</p>
  
  <p>My template looks like :</p>
  
  <pre><code>&lt;table class="lg-tableau-coeff"&gt;
                  &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;th&gt;ID&lt;/th&gt;
                      &lt;th&gt;Coefficient Technicien&lt;/th&gt;
                      &lt;th&gt;Coefficient Ingénieur&lt;/th&gt;
                      &lt;th&gt;Coefficient Consultant&lt;/th&gt;
                      &lt;th&gt;Coefficient Architecte&lt;/th&gt;
                      &lt;th&gt;Société&lt;/th&gt;
                  &lt;/tr&gt;
                  {% for item in coefficient %}
                  &lt;tr&gt;
                      &lt;td&gt;{{ item.id}}&lt;/td&gt;
                      &lt;td&gt;{{ item.Technicien}}&lt;/td&gt;
                      &lt;td&gt;{{ item.Ingenieur}}&lt;/td&gt;
                      &lt;td&gt;{{ item.Consultant }}&lt;/td&gt;
                      &lt;td&gt;{{ item.Architecte }}&lt;/td&gt;
                      &lt;td&gt;{{item.Societe.Nom}}&lt;/td&gt;
                  &lt;/tr&gt;
                  {% endfor %}
                  &lt;/tbody&gt;
              &lt;/table&gt;
  </code></pre>
  
  <p>How I can do that ?</p>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>With MySQL, the command is :</p>
  
  <p><code>SELECT * 
   FROM Identity_coefficientintervention
   WHERE id IN (SELECT MAX(id) FROM Identity_coefficientintervention GROUP BY Societe_id)</code></p>
  
  <p>So I have to find How I can translate it to Django Query</p>