8.3 KiB
Phase 4: Core Consolidation - Model Changes Summary
Date: 2025-10-06
This document summarizes all the model changes and fixes made during Phase 4 of the core consolidation effort.
1. Core Models (core/models.py)
New Models Created:
- Tenant - Multi-tenant support
- Patient - Core patient identity (replaces PatientProfile as primary)
- Encounter - Clinical encounters
- Facility - Healthcare facilities
- AuditEvent - Audit logging (renamed from AuditLogEntry)
- Attachment - File attachments
Key Changes:
- All models now inherit from
TenantScopedModelfor multi-tenancy - Patient model uses FHIR-aligned field names:
given_nameinstead offirst_namefamily_nameinstead oflast_namebirth_dateinstead ofdate_of_birthadministrative_genderinstead ofgender
- Added
external_idfield to all models for integration - Standardized timestamp fields:
created_at,updated_at - Standardized user tracking:
created_by,updated_by
2. Patients App (patients/models.py)
Model Structure Changes:
-
PatientProfile - Now extends core.Patient via OneToOneField
- Uses
patientas primary key (OneToOneField to core.Patient) - Removed fields that now exist in core.Patient:
first_name,last_name,date_of_birth,gender,id_number
- Kept extended demographics:
- Contact info, address, marital status, language preferences
- Healthcare info, allergies, advance directives
- VIP and confidential flags
- Uses
-
EmergencyContact - Links to core.Patient
- Changed FK from PatientProfile to core.Patient
- Maintains all contact and authorization fields
-
InsuranceInfo - Links to core.Patient
- Changed FK from PatientProfile to core.Patient
- Removed non-existent field:
subscriber_id_number - Kept all insurance-related fields
-
ConsentTemplate - No structural changes
- Inherits from TenantScopedModel
-
ConsentForm - Links to core.Patient
- Changed FK from PatientProfile to core.Patient
- Maintains all consent and signature fields
-
PatientNote - Links to core.Patient
- Changed FK from PatientProfile to core.Patient
- Maintains all note fields
3. EMR App (emr/models.py)
All models updated to link to core.Patient instead of patients.PatientProfile:
- ClinicalNote
- Vital
- Diagnosis
- Procedure
- Allergy
- Problem
- Observation
- CarePlan
Key Changes:
- All ForeignKey relationships changed from
patients.PatientProfiletocore.Patient - All models inherit from
TenantScopedModel - Standardized field names and audit fields
4. Forms Updates
patients/forms.py:
-
PatientProfileForm
- Removed fields that don't exist in PatientProfile:
first_name,last_name,date_of_birth,gender,id_number
- Only includes PatientProfile-specific fields:
- Extended demographics, contact info, healthcare preferences
- Removed fields that don't exist in PatientProfile:
-
InsuranceInfoForm
- Removed non-existent field:
subscriber_id_number - Added
is_primaryfield - Excluded
patientfield (set programmatically)
- Removed non-existent field:
-
EmergencyContactForm
- Excluded
patientfield (set programmatically)
- Excluded
-
PatientNoteForm
- Excluded
patientfield (set programmatically)
- Excluded
-
PatientsSearchForm
- Commented out
genderfield (now in core.Patient, not PatientProfile)
- Commented out
5. Views Updates
patients/views.py:
- Changed import from
AuditLogEntrytoAuditEvent - All views updated to work with new model structure
- Patient creation/update views need to handle both core.Patient and PatientProfile
core/views.py:
- Updated to use
AuditEventinstead ofAuditLogEntry
core/forms.py:
- TenantForm - Removed non-existent fields:
contact_person,contact_email,contact_phonesubscription_plan,subscription_status
6. Utils Updates
core/utils.py:
- AuditLogger class updated to use
AuditEventmodel - All logging methods updated to match new model structure
7. Blood Bank App
blood_bank/forms.py:
- Commented out non-existent models:
InventoryLocationForm(InventoryLocation model doesn't exist)QualityControlForm(QualityControl model doesn't exist)
- Removed imports for these models
blood_bank/views.py:
- Commented out views that reference non-existent models:
inventory_overview(uses InventoryLocation)quality_control_list(uses QualityControl)quality_control_create(uses QualityControl)
- Removed imports for these models
8. Database Migration Status
Current State:
- All migration directories have been cleared
- Database file (db.sqlite3) has been deleted
- Ready for fresh migrations once all fixes are verified
Next Steps (When Ready):
- Run
python manage.py makemigrationsto create initial migrations - Run
python manage.py migrateto apply migrations - Reseed data using updated data generation scripts
9. Key Architectural Changes
Multi-Tenancy:
- All models now properly scoped to tenants
TenantScopedModelbase class provides:tenantForeignKeyis_activeflagcreated_at,updated_attimestampscreated_by,updated_byuser trackingexternal_idfor integration
Patient Identity:
- core.Patient is now the canonical patient identity
- patients.PatientProfile extends Patient with healthcare-specific demographics
- All clinical and operational models link to core.Patient
Audit Trail:
- Renamed
AuditLogEntrytoAuditEventfor clarity - Standardized audit logging across all apps
AuditLoggerutility class provides consistent logging interface
10. Breaking Changes
For Existing Code:
-
Patient References: Change from
PatientProfiletoPatientin:- Foreign keys
- Queries
- View logic
-
Field Name Changes: Update references to:
first_name→given_namelast_name→family_namedate_of_birth→birth_dategender→administrative_gender
-
Audit Logging: Change from
AuditLogEntrytoAuditEvent -
Forms: Update form handling for split Patient/PatientProfile model
11. Files Modified
Core App:
core/models.py- Complete rewrite with new modelscore/forms.py- Updated TenantFormcore/views.py- Updated to use AuditEventcore/utils.py- Updated AuditLogger
Patients App:
patients/models.py- Restructured to extend core.Patientpatients/forms.py- Updated all forms for new structurepatients/views.py- Updated imports and logic
EMR App:
emr/models.py- Updated all FK relationships to core.Patient
Blood Bank App:
blood_bank/forms.py- Commented out non-existent model formsblood_bank/views.py- Commented out views for non-existent models
Other Apps:
- All apps updated to use core.Patient instead of patients.PatientProfile
- All apps updated to use AuditEvent instead of AuditLogEntry
12. Testing Checklist
Before running migrations, verify:
- All imports reference correct models
- All ForeignKey relationships point to correct models
- All forms only reference fields that exist in their models
- All views handle the Patient/PatientProfile split correctly
- All audit logging uses AuditEvent
- No references to deleted/non-existent models
13. Data Migration Considerations
When reseeding data:
- Create Tenant first
- Create core.Patient records
- Create PatientProfile records (linked to Patient)
- Create related records (EmergencyContact, InsuranceInfo, etc.)
- Ensure all tenant scoping is correct
- Verify audit trails are created properly
Summary
This phase successfully consolidated patient identity into core.Patient while maintaining extended demographics in patients.PatientProfile. All apps have been updated to use the new structure, and the codebase is ready for fresh migrations once all fixes are verified.
The key achievement is a cleaner separation of concerns:
- core.Patient: Canonical patient identity (FHIR-aligned)
- patients.PatientProfile: Extended healthcare demographics
- Clinical apps: All link to core.Patient for consistency
This structure provides a solid foundation for:
- Multi-tenant operations
- FHIR compliance
- Integration with external systems
- Comprehensive audit trails
- Scalable architecture