Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Models.py
Python
class Monitors(models.Model):
    name = models.CharField(max_length=255, blank=False)
    model = models.CharField(max_length=255, blank=True)

    aspect_ratio = models.CharField(max_length=255, blank=False)
    max_display_resolution = models.CharField(max_length=255,
                                                   blank=False)
    screen_size = models.CharField(max_length=255, blank= False)
    display_type= models.PositiveIntegerField( blank=True)
    refresh_rate = models.CharField(max_length=255, blank=False)
    mounting_type = models.CharField(max_length=255, blank= False)

    item_dimensions=models.CharField(max_length=255, blank=True)
    item_weight = models.PositiveIntegerField( blank=True)
    voltage = models.IntegerField(blank=False)
    color = models.CharField(max_length=50, blank=True)

    hdmi_port = models.FloatField(max_length=255, blank=False)
    built_speakers = models.CharField(max_length=255, blank=True)
    slug = models.SlugField(unique=True, blank=False)
    monitor_id = models.AutoField(primary_key=True, default=0)
    price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        validators=[
            MinValueValidator(limit_value=1,
            message='Price must be greater than or equal to 1.'),
            MaxValueValidator(limit_value=999999.99,
            message='Price cannot exceed 999999.99.'),
        ]
    )

    feature_1 = models.BooleanField(default=False)
    feature_2 = models.BooleanField(default=False)
    feature_3 = models.BooleanField(default=False)

    def __str__(self):
        return self.name

    class Meta:
        indexes = [
            models.Index(fields=['price']),
        ]

forms.py
Python
from django import forms
from monfilter.models import Monitors

class MonitorsForm(forms.ModelForm):
    class Meta:
        model = Monitors
        fields = '__all__'

views.py
Python
class MonitorListView(FilterView):
    model = Monitors
    template_name = 'monitor_list.html'
    context_object_name = 'monitors'
    paginate_by = 10
    filterset_class=MonitorsFilter

    def get_context_data(self, **kwargs):
        queryset = super().get_context_data(**kwargs)

        return queryset

filters.py
Python
import django_filters
from django import forms
from .models import Monitors

class MonitorsFilter(django_filters.FilterSet):
    feature_1 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
    feature_2 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
    feature_3 = django_filters.BooleanFilter(widget=forms.CheckboxInput)

    aspect_ratio = django_filters.CharFilter(
        lookup_expr='icontains',
        field_name='aspect_ratio')
    max_display_resolution = django_filters.CharFilter(
        lookup_expr='icontains',
        field_name='max_display_resolution')
    screen_size = django_filters.CharFilter(
        lookup_expr='icontains',
        field_name='screen_size')
    display_type = django_filters.CharFilter(
        lookup_expr='exact',
        field_name='display_type')
    refresh_rate = django_filters.CharFilter(
        lookup_expr='icontains',
        field_name='refresh_rate')
    mounting_type = django_filters.CharFilter(
        lookup_expr='icontains',
        field_name='mounting_type')

    class Meta:
        model = Monitors
        fields = []

urls.py
Python
from monfilter import views
from django.urls import path
from monfilter.views import MonitorListView

urlpatterns = [

    path('',views.HomePage,name='Home'),
    path('add_monitor/', views.add_monitor, name='add_monitor'),
    path('monitors/', MonitorListView.as_view(
        ), name='monitor_list'),
]

monitor_list.html
HTML
<!DOCTYPE html>
<html>
<head>
    <title>Monitor List</title>
</head>
<body>
    <h1>Monitor List</h1>

    <form method="get">
        {{ filter.form.as_p }}

        <button type="submit">Filter</button>
    </form>

    <ul>
        {% for monitor in monitors %}
            <li>{{ monitor.name }}</li>
            <!-- Include other fields you want to display -->
        {% empty %}
            <li>No monitors match the filter criteria.</li>
        {% endfor %}
    </ul>
</body>
</html>

Problem Statement:
Python
feature_1 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
feature_2 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
feature_3 = django_filters.BooleanFilter(widget=forms.CheckboxInput)

The three lines of code in the filter form are causing error. Error detail: when these fields are included in filter form, then only these fields of filter provide the correct search results. The other fields do not provide the correct search results [this message appears on the screen: "No matching Result found"]. When these fields are removed from the filter form, the other fields started working well by providing the correct results.

Secondly, when this code [widget=forms.CheckboxInput] is removed from these three lines of code, the whole filter form [all fields, other plus these three fields] works well, providing the correct results.

Question:

I want check box for these boolean fields. How can I display check-box for these boolean fields?

What I have tried:

I removed this code [widget=forms.CheckboxInput] and the filter form works well.
Python
feature_1 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
feature_2 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
feature_3 = django_filters.BooleanFilter(widget=forms.CheckboxInput)
Posted
Updated 5-Oct-23 0:35am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900