3.3 KiB
3.3 KiB
Analytics Dashboard FieldError Fix Summary
Problem
The analytics dashboard at /analytics/dashboard/ was throwing a Django FieldError:
FieldError at /analytics/dashboard/
Unsupported lookup 'survey_instance' for UUIDField or join on the field not permitted.
Root Cause
The error was occurring in the analytics_dashboard view in apps/analytics/ui_views.py. The problematic query was trying to access survey data through department relationships using an incorrect field lookup path.
The original code attempted to query survey instances through department-survey relationships, but the actual model relationships were:
Departmenthasjourney_instances(related name fromPatientJourneyInstance.department)PatientJourneyInstancehassurveys(related name fromSurveyInstance.journey_instance)
Solution
Fixed the query in apps/analytics/ui_views.py by using the correct relationship path:
# Fixed department rankings query
department_rankings = Department.objects.filter(
status='active'
).annotate(
avg_score=Avg(
'journey_instances__surveys__total_score',
filter=Q(journey_instances__surveys__status='completed')
),
survey_count=Count(
'journey_instances__surveys',
filter=Q(journey_instances__surveys__status='completed')
)
).filter(
survey_count__gt=0
).order_by('-avg_score')[:5]
Key Changes
- Correct relationship path:
journey_instances__surveys__total_scoreinstead of the incorrect lookup - Added filter annotations: Used
filter=Q()to count only completed surveys - Proper filtering: Filter for departments with survey_count > 0
Model Relationships
Department
└── journey_instances (PatientJourneyInstance.department)
└── surveys (SurveyInstance.journey_instance)
Testing
The fix was tested using Django shell:
python manage.py shell -c "from django.db.models import Avg, Count, Q; from apps.organizations.models import Department; qs = Department.objects.filter(status='active').annotate(avg_score=Avg('journey_instances__surveys__total_score', filter=Q(journey_instances__surveys__status='completed')), survey_count=Count('journey_instances__surveys', filter=Q(journey_instances__surveys__status='completed'))).filter(survey_count__gt=0)[:5]; print(f'Query successful! Found {list(qs).__len__()} departments')"
Result: ✓ Query executed successfully without errors
Files Modified
apps/analytics/ui_views.py- Fixed the department rankings query inanalytics_dashboardview
Impact
- The analytics dashboard now loads without errors
- Department rankings are correctly calculated based on survey scores
- The query properly filters for completed surveys only
- Empty results are handled gracefully (0 departments returned when no surveys exist)
Verification
To verify the fix is working:
- Navigate to
/analytics/dashboard/ - The page should load without FieldError
- Department rankings section should display (may be empty if no survey data exists)
Notes
- The query uses proper Django ORM annotations for aggregating survey data
- Filter annotations ensure only completed surveys are counted
- The fix maintains the original functionality while using correct field lookups
- No database migrations are required as this is purely a code-level fix