我已经使用以下设置应用了 django-filters 的基本示例:models.py
class Shopper(models.Model):
FirstName = models.CharField(max_length=30)
LastName = models.CharField(max_length=30)
Gender_CHOICES = (
('','---------'),
('Male','Male'),
('Female','Female'),
)
Gender = models.CharField(max_length=6, choices=Gender_CHOICES, default=None)
School_CHOICES = (
('','---------'),
(u'1', 'Primary school'),
(u'2', 'High School'),
(u'3', 'Apprenticeship'),
(u'4', 'BsC'),
(u'5', 'MsC'),
(u'6', 'MBA'),
(u'7', 'PhD'),
)
HighestSchool = models.CharField(max_length=40, blank = True, choices = School_CHOICES,default=None)
视图.py:
def shopperlist(request):
f = ShopperFilter(request.GET, queryset=Shopper.objects.all())
return render(request,'mysapp/emailcampaign.html', {'filter': f})
网址.py:
url(r'^emailcampaign/$', views.shopperlist, name='EmailCampaign'),
模板:
{% extends "basewlogout.html" %}
{% block content %}
{% csrf_token %}
<form action="" method="get">
{{ filter.form.as_p }}
<input type="submit" name="Filter" value="Filter" />
</form>
{% for obj in filter %}
<li>{{ obj.FirstName }} {{ obj.LastName }} {{ obj.Email }}</li>
{% endfor %}
<a href="{% url 'UserLogin' %}">
<p> Return to home page. </p> </a>
{% endblock %}
表格.py:
class ShopperForm(ModelForm):
class Meta:
model = Shopper
添加了空选项('','---------')
以确保 django-filters 将在过滤期间显示它并让该字段未指定。但是,对于非必填字段,在使用IeHighestSchool
创建场景中使用模型时,它会显示两次,不应为非必填字段列出。那么在过滤过程中无法选择empty_label...ModelForm.
('','---------')
如何通过在创建视图期间列出一个 empty_label 来解决此问题,并有可能在过滤期间未指定必填字段和非必填字段?