2.9 KiB
Analytics Dashboard FieldError Fix - Complete
Issue Summary
The analytics dashboard at /analytics/dashboard/ was throwing a FieldError:
Unsupported lookup 'survey_instance' for UUIDField or join on the field not permitted.
Root Cause Analysis
The error was in apps/analytics/ui_views.py at line 70, in the analytics_dashboard view. The code was attempting to perform a database lookup on a UUIDField that doesn't support the survey_instance lookup.
Problematic Code (Line 70):
).annotate(
survey_instance_count=Count('survey_instance__id'),
The issue was that the query was trying to annotate with a count of survey_instance__id, but the base queryset's relationship structure doesn't support this lookup path.
Fix Applied
Modified the query in apps/analytics/ui_views.py to remove the problematic annotation:
Before:
complaints_by_status = Complaint.objects.filter(
organization=request.user.organization
).annotate(
survey_instance_count=Count('survey_instance__id'),
)
After:
complaints_by_status = Complaint.objects.filter(
organization=request.user.organization
)
The survey_instance_count annotation was removed as it was causing the FieldError and wasn't being used in the template or view logic.
Additional Issue: Template Path Fix
After fixing the FieldError, a TemplateDoesNotExist error occurred for the KPI report templates. This was because they were extending base.html instead of layouts/base.html.
Templates Fixed:
templates/analytics/kpi_report_list.html- Changed{% extends 'base.html' %}to{% extends 'layouts/base.html' %}templates/analytics/kpi_report_generate.html- Changed{% extends 'base.html' %}to{% extends 'layouts/base.html' %}templates/analytics/kpi_report_detail.html- Changed{% extends 'base.html' %}to{% extends 'layouts/base.html' %}
Files Modified
apps/analytics/ui_views.py- Removed problematic annotationtemplates/analytics/kpi_report_list.html- Fixed template extends pathtemplates/analytics/kpi_report_generate.html- Fixed template extends pathtemplates/analytics/kpi_report_detail.html- Fixed template extends path
Impact
- The analytics dashboard should now load without errors
- All KPI report pages should render correctly
- The change is minimal and doesn't affect the functionality of the dashboard
- The removed annotation was not being used in the view or template
Verification
To verify the fix:
- Navigate to
/analytics/dashboard/ - Verify the page loads without FieldError
- Navigate to
/analytics/kpi-reports/ - Verify the KPI report list loads without TemplateDoesNotExist error
- Test generating and viewing KPI reports
Next Steps
The analytics dashboard should now be fully functional. Consider reviewing if the survey_instance_count annotation is needed elsewhere in the codebase, and if so, implement it using a valid field lookup path.