我正在尝试创建日期范围过滤器,所以这是我的模型和过滤器:
class Caixa(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
data = models.DateField('Data')
total = models.DecimalField('Total', decimal_places=2, max_digits=10, null=True)
unidade = models.ForeignKey(Unidade, on_delete=models.CASCADE)
def __str__(self):
return "Caixa " + self.unidade.nome
class CaixaFilter(django_filters.FilterSet):
data_inicial = django_filters.DateFilter(field_name='data', lookup_expr='gte'),
data_final = django_filters.DateFilter(field_name='data', lookup_expr='lte')
class Meta:
model = Caixa
fields = ['data']
所以,在我的HTML中,我有以下内容:
<form method="get">
<div class="row">
<div class="col s6">
{% render_field filter.form.data_inicial class="datepicker" %}
</div>
<div class="col s6">
{% render_field filter.form.data_final class="datepicker" %}
</div>
</div>
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Buscar
</button>
</form>
但HTML中仅显示“ data_final ”字段。
答案 0 :(得分:0)
import django_filters
class CaixaFilter(django_filters.FilterSet):
data = django_filters.DateFromToRangeFilter()
class Meta:
model = Caixa
fields = ['data']
这样,您可以使用两个键 date_after
和 date_before
来过滤日期范围。
注意:后缀 after
和 before
属于 django-filter==2.x