Я использую пакет фильтров django для фильтрации данных на основе полей начала, окончания и класса взносов, определенных в моих моделях. Проблема в том, что если я выбираю только class_of_fields, данные фильтруются, но когда я выбираю start и end_date вместе с class_of_dues, никакие данные не фильтруются ниже, это мои коды
models.py
class DuesLevy(models.Model):
class_of_dues = models.CharField(max_length=30, default=options.CHOOSE, choices=options.CLASS_OF_DUES, blank=True)
payment_circle = models.CharField(max_length=30, default=options.CHOOSE, choices=options.PAYMENT_CIRCLE)
payment_option = models.CharField(max_length=30, default=options.CHOOSE, choices=options.PAYMENT_OPTION)
amount = models.DecimalField(max_digits=8, decimal_places=2)
transaction_id = models.CharField(max_length=30, null=True, blank=True, editable=False, default=my_rand)
payment_channel = models.CharField(max_length=30, default=options.CHOOSE, choices=options.PAYMENT_CHANNEL_TYPE)
payment_date = models.DateField()
start_date = models.DateField()
end_date = models.DateField()
date_recorded = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.amount
filter.py
from .models import DuesLevy, User
from django_filters import widgets
import django_filters
from django import forms
from django_select2 import forms as s2forms
from backend import options
class DuesFilter(django_filters.FilterSet):
class_of_dues = django_filters.CharFilter(
widget=s2forms.Select2Widget(choices=options.CLASS_OF_DUES,
attrs={'class':'form-control'}))
start_date = django_filters.DateFilter(field_name='payment_date',
widget=forms.DateInput(attrs={'class':'form-control', 'type':'date'}), lookup_expr='lt',label='Start Date')
end_date = django_filters.DateFilter(field_name='payment_date',
widget=forms.DateInput(attrs={'class':'form-control', 'type':'date'}), lookup_expr='gt',label='End Date')
user = django_filters.ModelChoiceFilter(queryset=User.objects.all(),
widget=s2forms.ModelSelect2Widget(
attrs={'class': 'form-control'},
model=User,
search_fields=['member_id__icontains'],
))
class Meta():
model = DuesLevy
fields = ('class_of_dues', 'start_date', 'end_date')
views.py
class ListMemberDues(LoginRequiredMixin, View):
login_url = '/backoffice/'
def get(self, request):
queryset = DuesLevy.objects.filter(user=request.user)
query_filter = DuesFilter(request.GET, queryset)
context = {
'form':query_filter.form,
'query':query_filter,
}
return render(request, 'dashboard/list-member-dues.html', context)
шаблонов. html
<form class="form-inline d-flex justify-content-around">
<div class="col-sm-3 my-1">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">Class Of Dues</div>
</div>
{{ form.class_of_dues }}
</div>
</div>
<div class="col-sm-3 my-1">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">Start Date</div>
</div>
{{ form.start_date }}
</div>
</div>
<div class="col-sm-3 my-1">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">End Date</div>
</div>
{{ form.end_date }}
</div>
</div>
<button type="submit" class="btn btn-success">Filter</button>
{% csrf_token %}
</form>
{% if request.user %}
<div class="row p-3 bg-light">
<div class="col-md-4">
<h3>Account Details</h3>
<h4>{{ request.user.first_name }} {{ single.user.last_name }}</h4>
<h4>Phone {{ single.user.phone1 }}</h4>
<h4>LCDA: {{ request.user.lcda }}</h4>
<h4>Zone: {{ request.user.zone }}</h4>
<h4>Area: {{ request.user.area }}</h4>
</div>
<div class="col-md-4">
<img style="width: 200px; height: auto;" src="{{ request.user.additionalprofile.get_image_url }}" alt="">
</div>
</div>
{% endif %}
<table class="table">
<tr>
<th>Payment Date</th>
<th>Categories</th>
<th>Amount</th>
<th>Channel</th>
<th>Transaction ID</th>
</tr>
{% if query %}
{% for q in query.qs %}
<tr>
<td>{{ q.date_recorded }}</td>
<td>{{ q.class_of_dues }}</td>
<td>₦{{ q.amount }}</td>
<td>{{ q.payment_channel }}</td>
<td>{{ q.transaction_id }}</td>
</tr>
{% endfor %}
{% endif %}
</table>