79 lines
3.3 KiB
Markdown
79 lines
3.3 KiB
Markdown
# 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:
|
|
- `Department` has `journey_instances` (related name from `PatientJourneyInstance.department`)
|
|
- `PatientJourneyInstance` has `surveys` (related name from `SurveyInstance.journey_instance`)
|
|
|
|
## Solution
|
|
Fixed the query in `apps/analytics/ui_views.py` by using the correct relationship path:
|
|
|
|
```python
|
|
# 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
|
|
1. **Correct relationship path**: `journey_instances__surveys__total_score` instead of the incorrect lookup
|
|
2. **Added filter annotations**: Used `filter=Q()` to count only completed surveys
|
|
3. **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:
|
|
|
|
```bash
|
|
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 in `analytics_dashboard` view
|
|
|
|
## 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:
|
|
1. Navigate to `/analytics/dashboard/`
|
|
2. The page should load without FieldError
|
|
3. 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 |