From 7c372d04bbff128cb9b4c52246af7d5dd2110274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sun, 8 Oct 2017 15:11:24 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20:=20enl=C3=A8ve=20le=20annotate=20du=20fo?= =?UTF-8?q?rm=20pour=20ne=20l'utiliser=20que=20dans=20le=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit annotate(...) duplique les ip quand un ip_type est lié à plusieurs machine_type donc le form avait plusieurs fois la même ip (même id) dans les résultats de son queryset --- machines/forms.py | 18 +++++++++--------- machines/views.py | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index 48498d36..63539888 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -29,7 +29,7 @@ import re from django.forms import ModelForm, Form, ValidationError from django import forms from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, OuverturePortList, OuverturePort -from django.db.models import Q, F +from django.db.models import Q from django.core.validators import validate_email from users.models import User @@ -63,9 +63,9 @@ class EditInterfaceForm(ModelForm): self.fields['type'].empty_label = "Séléctionner un type de machine" if "ipv4" in self.fields: self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance) if "machine" in self.fields: self.fields['machine'].queryset = Machine.objects.all().select_related('user') @@ -79,9 +79,9 @@ class AddInterfaceForm(EditInterfaceForm): self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" if not infra: self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False)) - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)) else: - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True) class NewInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): @@ -97,12 +97,12 @@ class BaseEditInterfaceForm(EditInterfaceForm): self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" if not infra: self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False)) - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance) else: - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id')) - self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance) class AliasForm(ModelForm): class Meta: diff --git a/machines/views.py b/machines/views.py index 0ebde631..ac37d8c6 100644 --- a/machines/views.py +++ b/machines/views.py @@ -36,7 +36,7 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.template import Context, RequestContext, loader from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required -from django.db.models import ProtectedError +from django.db.models import ProtectedError, F from django.forms import ValidationError, modelformset_factory from django.db import transaction from django.contrib.auth import authenticate, login @@ -92,7 +92,7 @@ def generate_ipv4_choices( form ) : choices = '{"":[{key:"",value:"Choisissez d\'abord un type de machine"},' mtype_id = -1 - for ip in f_ipv4.queryset.order_by('mtype_id', 'id') : + for ip in f_ipv4.queryset.annotate(mtype_id=F('ip_type__machinetype__id')).order_by('mtype_id', 'id') : if mtype_id != ip.mtype_id : mtype_id = ip.mtype_id used_mtype_id.append(mtype_id)