Hi,

I'm currently learning Django and I've tried the autocomplete-light 
framework base on Select2.

The problem i'm facing is that I want to Forward input field information to 
filter another input field.

There is a lot of information about that on the internet and on the 
autocomplete-light tutorial but, every example that I've seen use the same 
url address to make there Foward. I was wondering if it was possible to 
Forward it to another address ?

I can change the url adress but it make my first input field return element 
of the second.

In the code below, want I want is to filter codePostalResidence base on the 
selected villeMunicipaliteResidence value.

I want the form to filter base on the link "cmunic = munic_id"

I know that's a lot of information, but if someone can give me a hint about 
anything it would really help me.

Thanks!

#forms.py

class ODData2018Form(forms.ModelForm):

    villeMunicipaliteResidence = forms.ModelChoiceField(label='Ville / 
Municipalité : ', required=False,
        queryset=TbMunicsV01.objects.all(),
        widget=autocomplete.ModelSelect2(url='municipalite2001', 
attrs={'data-placeholder': 'Ville / Municipalité', 'data-html': True, 
'style':'width:100%'}, forward=['villeMunicipaliteResidence'])

    codePostalResidence = forms.ModelChoiceField(label='Code postal : ', 
required=False,
        queryset=TbCpV01.objects.all(),
        widget=autocomplete.ModelSelect2(url='codepostal',
    attrs={
        'data-placeholder': 'Code postal',
    },)
    )
    )

#views.py

class CodePostal(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        #if not self.request.user.is_authenticated:
        #    return Choice.objects.none()

        qs = TbCpV01.objects.all()

        villeMunicipaliteResidence = 
self.forwarded.get('villeMunicipaliteResidence', None)
        print(str(villeMunicipaliteResidence))
        if villeMunicipaliteResidence:
            qs = qs.filter(cmunic=villeMunicipaliteResidence)

        if self.q:
            qs = qs.filter(cp__istartswith=self.q)

        return qs

class Municipalite2001(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        #if not self.request.user.is_authenticated:
        #    return TbMun2001HV01.objects.none()

        qs = TbMun2001HV01.objects.all()

        if self.q:
            qs = qs.filter(nom_mun__istartswith=self.q)

        return qs

#models.py

class TbCpV01(models.Model):
    cp_idu = models.IntegerField(blank=True, null=True)
    cp = models.TextField(blank=True, null=True)
    pr = models.IntegerField(blank=True, null=True)
    prabv = models.TextField(blank=True, null=True)
    sdr2016c = models.TextField(blank=True, null=True)
    cmunic = models.IntegerField(blank=True, null=True)
    drsdr2016c = models.TextField(blank=True, null=True)
    inod18mtl4 = models.IntegerField(blank=True, null=True)
    inod18mtl = models.IntegerField(blank=True, null=True)
    xll83 = models.FloatField(blank=True, null=True)
    yll83 = models.FloatField(blank=True, null=True)
    xmtm83z8 = models.FloatField(blank=True, null=True)
    ymtm83z8 = models.FloatField(blank=True, null=True)
    visible = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return self.cp

class TbMun2001HV01(models.Model):
    munic_id = models.IntegerField(blank=True, null=True)
    sdr2001h = models.TextField(blank=True, null=True)
    drsdr2001h = models.TextField(blank=True, null=True)
    nom_mun = models.TextField(blank=True, null=True)
    desg_mun_a = models.TextField(blank=True, null=True)
    desg_mun = models.TextField(blank=True, null=True)
    drsdr2016c = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.nom_mun

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4674f877-530b-451b-8d5b-10d9412a7cc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to