Django-smart-selects doesn't work in FilterSet

Let me start by saying that I am truly a beginner in Django.

But I have already tried to search for possible solutions on the Internet and I still haven’t solved my problem. In fact, in the template that uses the form based on the “Interventions” model django-smart-selects work. While in the template that uses the form based on the “InterventionFilter”, the Plants are not filtered by the Company.

Here is my code:

models.py

class Company(models.Model):
    ID = models.AutoField(primary_key=True, unique=True)
    company_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=100, blank=True)
    email = models.EmailField(max_length=100, blank=True)

class Plant(models.Model):
    ID = models.AutoField(primary_key=True)
    plant_name = models.CharField(max_length=100)
    ID_company = models.ForeignKey(Company, on_delete=models.CASCADE)

class Intervention(models.Model):
    ID = models.AutoField(primary_key=True)
    start_date = models.DateField()
    description = models.TextField()
    ID_company = models.ForeignKey(Company, on_delete=models.CASCADE)
    ID_plant = ChainedForeignKey(
        Plant,
        chained_field="ID_company",
        chained_model_field="ID_company",
        show_all=False,
        auto_choose=True,
        sort=True)

filters.py

import django_filters
from django_filters import DateFilter, CharFilter
from django.forms import DateInput

from .models import Intervention

class InterventionFilter(django_filters.FilterSet):
    date1 = DateFilter(field_name='start_date', lookup_expr='gte', label='From', widget=DateInput(attrs={'type': 'date'})) #GTE
    date2 = DateFilter(field_name='start_date', lookup_expr='lte', label='To', widget=DateInput(attrs={'type': 'date'})) #LTE
    descr = CharFilter(field_name='description', lookup_expr='icontains', label='Description')
    class Meta:
        model = Intervention
        exclude = ['start_date', 'description'] #(date1, date2, descr)
        fields = ['date1',
                  'date2',
                  'ID_company',
                  'ID_plant', 
                  'descr']

html

{% extends 'base.html'%}
{% load crispy_forms_tags %}
{% block content %}
<form method="post" action="{% url 'riepilogo' %}">
    {% csrf_token %}
    <div class="d-print-none">
            <div class="container border border-dark rounded p-3">
                    <div class="row">
                        <div class="col-md-6 col-6">{{ filtri.form.date1|as_crispy_field }}</div>
                        <div class="col-md-6 col-6">{{ filtri.form.date2|as_crispy_field }}</div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-12 col-12">{{ filtri.form.ID_company|as_crispy_field }}</div>
                    </div>

                    <div class="row">
                        <div class="col-md-12 col-12">{{ filtri.form.ID_plant|as_crispy_field }}</div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-12 col-12">{{ filtri.form.descr|as_crispy_field }}</div>
                    </div>
                    
                    <br>
                    
                    <div class="form-row">
                        <div class="col-auto text-center"><button type="submit" class="btn btn-primary">Search</button></div>
                    </div>
                </div>
    </div>
</form>
{% endblock %}

How do I make Django Smart Select work in the filter?

Sorry for the bad English.

Thank you all very much for your very kind, constant and indispensable help.

How about your views.py files, can we see it?

1 Like

Good morning @Blaise-93. Thank you for your question and sorry for the long delay in responding to you.
Here is the view regarding the FilterSet:

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .filters import InterventionFilter

@login_required
def riepilogo(request):
    tabella = Intervention.objects.all().values('ID',
                                              'start_date',
                                              'ID_plant__plant_name',
                                              'description').order_by('start_date')

    filtri = InterventionFilter(request.POST, queryset=tabella)
    tabellaFiltrata = filtri.qs

    return render(request, 'riepilogo.html', {'filtri': filtri,
                                              'tabellaFiltrata': tabellaFiltrata})

Maybe there is something to include in the view. But I still can’t find any information on the internet.

Without you making yourselves available in the Forum, I probably wouldn’t have even managed to finish half the Django project. So I thank you very much @Blaise-93.

And I thank everyone.