Skip to content

Simple Problem - Creating interdependent dropdowns using Select2 and Django-filter #55

@james-miles

Description

@james-miles

Goal
I have one model called Car. My aim is to create two dropdowns for 'make and 'model' fields for which the dropdown choices are distinct and interdependent between each other. When one choice is made then the other will filter its possible choices respectively (interdependent).

Problem
I am very much a novice to Django and Select2, so forgive me if the answer is obvious but I have been struggling for some time now. I cannot seem to render a form that shows any fields. This may also be an issue with my html code so it would be great to know how to show this effectively in html.

Code Snippet

                        import django_filters
                        from.models import Car
                        from django_select2 import forms as s2forms
                        from django_select2.forms import Select2Widget
                        from django_select2.forms import ModelSelect2Widget
                          
                          
                          
                        
                        class MyWidget (s2forms.ModelSelect2Widget):
                        
                            search_fields = [
                                'make__icontains','model__icontains',
                        
                            ]





                        class CarFilter (django_filters.FilterSet):
                          make = django_filters.ModelChoiceFilter(queryset=Car.objects.values('make').distinct(), widget=MyWidget)
                          model = django_filters.ModelChoiceFilter(queryset=Car.objects.values('model').distinct(), widget=MyWidget)
                        
                            class Meta:
                                model = Car
                                fields = ['make','model']

my views.py

                          from django.shortcuts import render
                          from .models import Car
                          from django.db.models import Count
                          from .filters import CarFilter
                          
                          def index(request):
                              all = Car.objects.all()
                              myFilter = CarFilter(request.GET, queryset=all)
                              all = myFilter.qs

                          context = {
                         
                              'all': all,
                              'myFilter' :myFilter,
                      
                          }
                      
                      
                          return render(request, 'index.html', context)

my html

                      <form method="get" action="">
                        <div class="col-md-3 col-sm-4">
                            <select class="selectpicker topsearch" data-width="100%" >
                                <option value = {{ myFilter.form.make  }}></option>
                            </select>
                            <select class="selectpicker topsearch" data-width="100%" >
                                <option value = {{ myFilter.form.model }}></option>
                            </select>
                        </div>
                      </form>

Any help would be greatly appreciated! thank-you

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions