Django - Get distinct data from field -
i'm looking getting distinct objects database distinct()
command.
i have model :
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)
my form looks :
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']
and view simple :
@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 é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)
my table has 3 lines example :
if print coefficient
, : <queryset [{'societe': 2l}, {'societe': 3l}]>
but display distinct objects in array , take last 1 each time.
my template looks :
<table class="lg-tableau-coeff"> <tbody> <tr> <th>id</th> <th>coefficient technicien</th> <th>coefficient ingénieur</th> <th>coefficient consultant</th> <th>coefficient architecte</th> <th>société</th> </tr> {% item in coefficient %} <tr> <td>{{ item.id}}</td> <td>{{ item.technicien}}</td> <td>{{ item.ingenieur}}</td> <td>{{ item.consultant }}</td> <td>{{ item.architecte }}</td> <td>{{item.societe.nom}}</td> </tr> {% endfor %} </tbody> </table>
how can ?
update :
with mysql, command :
select * identity_coefficientintervention id in (select max(id) identity_coefficientintervention group societe_id)
so have find how can translate django query
Comments
Post a Comment