HH/COMMAND_CENTER_ANNOTATION_FIX.md

64 lines
1.9 KiB
Markdown

# Command Center Annotation Fix
## Problem
When visiting the Command Center page at `/analytics/command-center/`, the following error occurred:
```
ValueError: The annotation 'patient_name' conflicts with a field on the model.
```
## Root Cause
The error occurred in `apps/analytics/ui_views.py` in the `command_center_api` function. The code was using `.annotate()` to create a computed field named `patient_name` by concatenating the patient's first and last name:
```python
.values(
'id',
'title',
'severity',
'due_at',
hospital_name=F('hospital__name'),
department_name=F('department__name'),
patient_name=Concat('patient__first_name', Value(' '), 'patient__last_name') # ❌ Error here
)
```
However, the `Complaint` model already has a field named `patient_name` (likely for storing the patient name directly in the complaint metadata). Django doesn't allow annotations to have the same name as existing model fields.
## Solution
Renamed the annotation from `patient_name` to `patient_full_name` to avoid the conflict:
### Changes Made
1. **apps/analytics/ui_views.py - `command_center_api` function**:
- Changed annotation name from `patient_name` to `patient_full_name` in the `.values()` call
2. **apps/analytics/ui_views.py - `export_command_center` function**:
- Changed annotation name from `patient_name` to `patient_full_name` in the `.annotate()` call
- Updated `.values_list()` to use `patient_full_name`
3. **templates/analytics/command_center.html**:
- Updated JavaScript to reference `complaint.patient_full_name` instead of `complaint.patient_name`
## Code Changes
### Before (❌)
```python
.values(
'id',
'title',
'severity',
'due_at',
hospital_name=F('hospital__name'),
department_name=F('department__name'),
patient_name=Concat('patient__first_name', Value(' '), 'patient__last_name')
)
```
### After (✅)
```python
.values(
'id',
'title',
'severity',
'due_at',