Django-smart-select doesn't work in FilterSet

32 Views Asked by At

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.

0

There are 0 best solutions below