def update(self, hosts): """Restrict `ModelChoiceFields` to values existing in hosts queryset""" # To avoid some useless select_related joins, only is explicitly used if 'os' in self.fields and not self.cleaned_data['os']: self.fields['os'].queryset = \ OperatingSystem.objects.filter(host__in=hosts) \ .only('id', 'name', 'version').distinct() if 'status' in self.fields and not self.cleaned_data['status']: self.fields['status'].queryset = \ HostStatus.objects.filter(host__in=hosts) \ .only('id', 'name').distinct() if 'supplier' in self.fields and not self.cleaned_data['supplier']: self.fields['supplier'].queryset = \ Supplier.objects.filter(host__in=hosts) \ .only('id', 'name').distinct() if 'type' in self.fields and not self.cleaned_data['type']: self.fields['type'].queryset = \ HostType.objects.filter(host__in=hosts) \ .only('id', 'text').distinct() if 'site' in self.fields and not self.cleaned_data['site']: self.fields['site'].choices = chain((('', ''),), ((c.id, str(c)) for c in sort_queryset( Client.objects.filter(host__in=hosts) .only('id', 'label', 'parent', 'parent__parent') .distinct() ) ))
def get_form(self, request, obj=None, **kwargs): """ Override get_form to add "client" to project """ form = super(BonDeCommandeAdmin, self).get_form(request, obj, **kwargs) form.base_fields["client"] = df.ChoiceField( choices=[(x.pk, x) for x in sort_queryset(request.user.clients)], widget=FilteringSelect(), required=True) return form
def __init__(self, user, *args, **kwargs): super(SearchHostForm, self).__init__(*args, **kwargs) ordered_clients = \ sort_queryset(Client.objects .filter(id__in=(c.id for c in user.clients))) self.fields['site'].choices = \ [('', '')] + [(c.id, unicode(c)) for c in ordered_clients] self._security_filter(user)
def __init__(self, user, ip, *args, **kwargs): "Save some data for logging and initialise fields" super(HostForm, self).__init__(*args, **kwargs) self.fields['site'].choices = ((c.id, str(c)) for c in sort_queryset(Client.objects.filter( id__in=(c.id for c in user.clients)))) self.initial["date_start_prod"] = datetime.date.today() self._security_filter(user) self.user = user self.user_ip = ip self.is_new = self.instance.pk is None # LC: On filtre les colones "depleted" if self.is_new: self.fields['os'].queryset = self.fields['os'].queryset.filter(depleted=False) self.fields['supplier'].queryset = self.fields['supplier'].queryset.filter(depleted=False)
def get_clients(self): if self.user.is_superuser: return sort_queryset(Client.objects.all()) if self.client: return sort_queryset(Client.objects.get_childs("parent", self.client.pk)) return Client.objects.none()