0

I have a model Property

class Property(models.Model):

PROPERTY_CATEGORIES = (
('flat/apartment 1BHK','flat/apartment 1BHK'),
('flat/apartment 2BHK','flat/apartment 2BHK'),
('flat/apartment 3BHK','flat/apartment 3BHK'),
('house','house'),
('pg/hostel +1 mate','pg/hostel +1 mate'),
('pg/hostel +2 mate','pg/hostel +2 mate'),
('pg/hostel +3 mate','pg/hostel +3 mate'),
('pg/hostel +4 mate','pg/hostel +4 mate'),    
)

name        = models.CharField(max_length=200)
proptype    = models.CharField(max_length=100, choices=PROPERTY_CATEGORIES)

and my filters.py looks like this

import django_filters
from .models import Property
from django.db import models
from django import forms

class PropertyFilter(django_filters.FilterSet):
    PROPERTY_CATEGORIES = (
    ('flat/apartment 1BHK','flat/apartment 1BHK'),
    ('flat/apartment 2BHK','flat/apartment 2BHK'),
    ('flat/apartment 3BHK','flat/apartment 3BHK'),
    ('house','house'),
    ('pg/hostel +1 mate','pg/hostel +1 mate'),
    ('pg/hostel +2 mate','pg/hostel +2 mate'),
    ('pg/hostel +3 mate','pg/hostel +3 mate'),
    ('pg/hostel +4 mate','pg/hostel +4 mate'),    
    )
    proptype = django_filters.ChoiceFilter(choices=PROPERTY_CATEGORIES)
    class Meta:
        model = Property
        fields = ['proptype']
        filter_overrides = {
                         models.CharField:{
                 'filter_class' : django_filters.ChoiceFilter,
                 'extra' : lambda f: {
                     'widget' : 'forms.CheckboxInput'
                 }
             }
        }

and my html file is

{% block content %}
    <form method="get">
        <!-- {{ filter.form.as_p }} -->
        {% for choice in filter.form.proptype %}
        {{ choice.choice_label }}
        <span class="radio">{{ choice.tag }}</span>
        {% endfor %}
        <input type="submit" />
    </form>
    {% for obj in filter.qs %}
        {{ obj }} <br />
    {% endfor %}
{% endblock %}

I want to display the choices like as radiobuttons or checkboxes

like a list of categories like house, flat etc like with radiobuttons to select them

but I cant find a way to do this please help

1 Answers1

1

forms.CheckboxInput only renders one checkout out, used mainly for boolean(True or False) selection. you need to use forms.CheckboxSelectMultipleor forms.RadioSelect instead.

class PropertyFilter(django_filters.FilterSet):
    ...
    proptype = django_filters.ChoiceFilter(choices=PROPERTY_CATEGORIES)
    class Meta:
        model = Property
        fields = ['proptype']
        filter_overrides = {
            models.CharField:{
                 'filter_class' : django_filters.ChoiceFilter,
                 'extra' : lambda f: {
                      #'widget' : forms.RadioSelect
                      'widget' : forms.CheckboxSelectMultiple
                 }
             }
        }

for further customization you can use a customized or builtin widgets and pass attributes like placeholder, class to control how the form looks, which has the same API with forms. Internally, Filter class hold a default form Field as a class attribute, paramters defined on the Filter related to form are passed to form Field to render the HTML.

for example:

from django.forms.widgets import Input

django_filters.CharFilter(label='name_starts', method=filter_name_starts, 
    widget = Input(attrs={'placeholder':'filter by first letter...', 'class': 'form-control'}))
minglyu
  • 2,958
  • 2
  • 13
  • 32