Reputation: 5793
I'm trying to add some context to a Django admin view when the change list is displayed.
I have this class
class LeadStatSummaryAdmin(admin.ModelAdmin):
change_list_template = 'admin/stats/leadstatsummary/change_list.html'
def get_queryset(self, request):
qs = super().get_queryset(request)
query=Q()
if 'from_date' in request.GET:
from_date = datetime.datetime.strptime(request.GET['from_date'], '%d/%m/%Y').strftime('%Y-%m-%d')
to_date = datetime.datetime.strptime(request.GET['to_date'], '%d/%m/%Y').strftime('%Y-%m-%d')
query = Q(date_of_lead__gte=from_date, date_of_lead__lte=to_date)
return qs.filter(query)
def changelist_view(self, request, extra_context=None):
response = super().changelist_view(
request,
extra_context=extra_context,)
qs = self.get_queryset(request)
response.context_data['date_form'] = DateForm(request.GET or None)
response.context_data['data'] = qs. \
values('type_of_lead', 'may_contact_provider', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village') \
.order_by('type_of_lead', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village','may_contact_provider') \
.annotate(count=Count('type_of_lead'))
return response
This provides a date form I can use to filter the queryset. This runs fine when called from the menu (so no date form) but when I enter dates and submit I get this error
'HttpResponseRedirect' object has no attribute 'context_data'
It is referring to this line of code
response.context_data['date_form'] = DateForm(request.GET or None)
I do not understand why this should be causing an error and how to fix. Can you help please
Upvotes: 3
Views: 1263
Reputation: 5793
For anyone interested I resolved using a template tag instead
@register.filter
def get_lead_summary(qs):
return qs.values('type_of_lead', 'may_contact_provider', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village') \
.order_by('type_of_lead', 'type_of_care_care_home', 'type_of_care_home_care',
'type_of_care_live_in_care', 'type_of_care_retirement_village','may_contact_provider') \
.annotate(count=Count('type_of_lead'))
This is called with, for example, {% for row in cl.result_list|get_lead_summary %}
Upvotes: 4