Django Autocomplete Light, forward widget is not working

27 Views Asked by At

I have the following code:

forms.py

from dal import autocomplete, forward

class AnalysisForm(forms.Form):
    def __init__(self, *args, **kwargs):
       super().__init__(*args, **kwargs)

    class Meta:
       model = Analysis
       fields = ['method', 'database']
   
    method_type = forms.ChoiceField(choices=[('', ' -- select an option -- ', 'A', 'B')], required=True))
    database = forms.ModelChoiceField(queryset=Database.objects.all(),
                                         required=True,
                                         widget=autocomplete.ModelSelect2(url='database-autocomplete',
                                                                          forward=("method_type", ), 
                                                                          attrs={'data-placeholder': 'Database ...',
                                                                                 'data-minimum-input-length': 1.,
                                                                                 },
                                                                         )
                                   )

views.py

class DatabaseAutocomplete(autocomplete.Select2QuerySetView): # url: database-autocomplete
    def get_queryset(self):
        qs = Database.objects.all()

        print(self.forwarded)

        script_type = self.forwarded.get('method_type')
        print("SCRIPT CHOICE")
        print(script_type)

        if script_type is None:
            qs = Database.objects.none()

What this code is doing is is that the form takes a value for method_type and database (which uses autocomplete). I would like database to access the value of method_type when running the autocomplete function but it is not being sent (notice how I am printing self.forwarded, but it is always empty). I have also produced some variations on the above method for example: forward=(forward.Field("method_type"), ) which did not work. I also used: forward=(forward.Const(42, "b"), ) to try and send anything which also failed. Am I missing anything? I would appreciate some help.

0

There are 0 best solutions below