Hi All,
I have implemented several filters using django_filters library, however, I’m trying to filter dates, so essentially I want to be able to filter my queryset starting/ending at certain dates:
Filters.py
class LeadEntriesFilter(django_filters.FilterSet):
start_date = DateFilter(field_name='date',
widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
lookup_expr='lt', label='Start Date')
end_date = DateFilter(field_name='date',
widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
lookup_expr='gt', label='End Date')
date_range = DateRangeFilter(field_name='date')
class Meta:
model = LeadEntry
fields = ['start_date','end_date','date_range']
Views
def sales_forecast(request):
# Gets the LeadEntry
forecast = LeadEntry.objects.all().select_related('lead_id')
forecast_filter = LeadEntriesFilter(request.GET, queryset=forecast)
forecast = forecast_filter.qs
print(request.GET) # Showing the output below
print(forecast)# Showing the output below
if not forecast:
return HttpResponse('<h1>Error</h1>')
final_df = read_frame(forecast)
final_df['Invoice'] = final_df['revenue']*final_df['probability']
revenue = [float(i) for i in final_df["revenue"]]
label = [i for i in final_df["stage"]]
year = [i.year for i in final_df["date"]]
month = [i.month for i in final_df["date"]]
data = {'Year':year,
'Month': month,
'Revenue':revenue,
'Stage':label}
result = pd.DataFrame(data, columns=["Year","Month" ,"Revenue", "Stage"])
table_date = result.groupby(["Year","Month","Stage"]).sum()
table_date.reset_index(inplace=True)
table_date.sort_values(by=["Year","Month"], inplace=True)
table_date["Month"] = table_date["Month"].apply(lambda x: calendar.month_abbr[x])
table_date["Date"] = table_date["Month"] + "-" + table_date["Year"].astype(str)
table_dict_leads = {'Date':[i for i in table_date["Date"][table_date["Stage"]=="Lead"]],
'Revenue':[j for j in table_date["Revenue"][table_date["Stage"]=="Lead"]],
'Stage':[k for k in table_date["Stage"][table_date["Stage"]=="Lead"]]}
table_dict_deals = {'Date': [i for i in table_date["Date"][table_date["Stage"] == "Deal"]],
'Revenue': [j for j in table_date["Revenue"][table_date["Stage"] == "Deal"]],
'Stage': [k for k in table_date["Stage"][table_date["Stage"] == "Deal"]]}
return render(request, "account/sales_forecast_python.html", context= {'leads':table_dict_leads,
'deals':table_dict_deals,
'filter':forecast_filter})
I have created an error handling for the moment, but essentially what is happening is that after applying the Start Date and End Date, my quersyet is returning an empty dataset, which is extremely strange, since I have a lot of data for that period:
<QueryDict: {‘start_date’: [‘2021-12-01’], ‘end_date’: [‘2022-12-19’], ‘date_range’: [’’]}>
<QuerySet []>
I believe that my implementation of the filter.py logic is perhaps not the best, from the documentation, I have seen that most examples are written with that syntax.
The quersyet is not empty if I only put the start date, but the results are still incorrect
Regards,
FCS