【发布时间】:2020-10-14 15:48:47
【问题描述】:
我有一个模特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)
我的 filters.py 看起来像这样
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'
}
}
}
我的html文件是
{% 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 %}
我想将选项显示为单选按钮或复选框
喜欢 a list of categories like house, flat etc like with radiobuttons to select them
但我找不到方法,请帮忙
【问题讨论】:
标签: python django django-forms django-filter django-widget