# 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): ```python ).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: ```python complaints_by_status = Complaint.objects.filter( organization=request.user.organization ).annotate( survey_instance_count=Count('survey_instance__id'), ) ``` ### After: ```python 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: 1. `templates/analytics/kpi_report_list.html` - Changed `{% extends 'base.html' %}` to `{% extends 'layouts/base.html' %}` 2. `templates/analytics/kpi_report_generate.html` - Changed `{% extends 'base.html' %}` to `{% extends 'layouts/base.html' %}` 3. `templates/analytics/kpi_report_detail.html` - Changed `{% extends 'base.html' %}` to `{% extends 'layouts/base.html' %}` ## Files Modified 1. `apps/analytics/ui_views.py` - Removed problematic annotation 2. `templates/analytics/kpi_report_list.html` - Fixed template extends path 3. `templates/analytics/kpi_report_generate.html` - Fixed template extends path 4. `templates/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: 1. Navigate to `/analytics/dashboard/` 2. Verify the page loads without FieldError 3. Navigate to `/analytics/kpi-reports/` 4. Verify the KPI report list loads without TemplateDoesNotExist error 5. 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.