1.9 KiB
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:
.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
-
apps/analytics/ui_views.py -
command_center_apifunction:- Changed annotation name from
patient_nametopatient_full_namein the.values()call
- Changed annotation name from
-
apps/analytics/ui_views.py -
export_command_centerfunction:- Changed annotation name from
patient_nametopatient_full_namein the.annotate()call - Updated
.values_list()to usepatient_full_name
- Changed annotation name from
-
templates/analytics/command_center.html:
- Updated JavaScript to reference
complaint.patient_full_nameinstead ofcomplaint.patient_name
- Updated JavaScript to reference
Code Changes
Before (❌)
.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 (✅)
.values(
'id',
'title',
'severity',
'due_at',