Allergy intolerance
Technical reference for the AllergyIntolerance module in Care EMR. For the plain-language view, see the Allergy intolerance concept.
Source:
- Model:
care/emr/models/allergy_intolerance.py - Specs:
care/emr/resources/allergy_intolerance/spec.py·valueset.py - Viewset:
care/emr/api/viewsets/allergy_intolerance.py - Shared bases:
care/emr/resources/base.py·care/emr/api/viewsets/base.py
The Django model is the storage layer. It stores the allergen (code) and the onset (onset) as JSON fields, and the coded fields as text columns.
The Pydantic resource specs give the JSON fields their shape. They also define the enums, the required fields, the bound value set, and the read and write schemas.
The viewset defines the API actions, the authorization checks, and the copy that Care keeps on the old encounter when an update moves an allergy.
Models
| Model | Purpose |
|---|---|
AllergyIntolerance | One allergy of one patient. It is linked to one encounter: the encounter named in its create request or in its latest update. A copy stays on the encounter that the original was linked to before an update moved it. |
AllergyIntolerance extends EMRBaseModel. Through it, each allergy has external_id, created_date, modified_date, the soft-delete flag deleted, created_by, updated_by, history and meta.
AllergyIntolerance fields
Coded fields
| Field | Storage | Null / default | Spec type | Notes |
|---|---|---|---|---|
clinical_status | CharField(100) | null=True, blank=True | ClinicalStatusChoices on write, str on read | Required on create and on update |
verification_status | CharField(100) | null=True, blank=True | VerificationStatusChoices on write, str on read | Required on create and on update |
category | CharField(100) | null=True, blank=True | CategoryChoices on create, str on read | Required on create. Not in the update spec, so it cannot change after creation |
criticality | CharField(100) | null=True, blank=True | CriticalityChoices on write, str on read | Required on create and on update |
allergy_intolerance_type | CharField(20) | default="allergy" | AllergyIntoleranceTypeOptions on write, str on read | Optional on create and on update; the spec default is allergy |
Allergen
| Field | Storage | Null / default | Spec type | Notes |
|---|---|---|---|---|
code | JSONField | default=dict, null=False, blank=False | ValueSetBoundCoding bound to system-allergy-code on create, Coding on read | Required on create. Not in the update spec, so it cannot change after creation. Shape and value set below |
Timing
| Field | Storage | Null / default | Spec type | Notes |
|---|---|---|---|---|
onset | JSONField | default=dict | AllergyIntoleranceOnSetSpec | Optional on create, default {}. Not in the update spec, so it cannot change after creation. Shape below |
recorded_date | DateTimeField | null=True, blank=True | datetime | None | Optional on create. Not in the update spec, so it cannot change after creation |
last_occurrence | DateTimeField | null=True, blank=True | datetime | None | Optional on create and on update |
Links, note and copies
| Field | Storage | Null / default | Spec type | Notes |
|---|---|---|---|---|
patient | FK → emr.Patient | on_delete=CASCADE | Not in any spec | Set on create to the patient of the encounter in the request |
encounter | FK → emr.Encounter | on_delete=CASCADE | UUID4 (the encounter's external_id) | Required on create and on update. The encounter must exist. An update links the allergy to the encounter in the request |
note | TextField | null=True, blank=True | str | None | Optional on create and on update |
copied_from | BigIntegerField | default=None, null=True, blank=True | Not in any spec | Internal ID of the original allergy when this row is a copy; null for an original. Set by Care when an update moves an allergy to another encounter |
Inherited from EMRBaseModel
| Field | Storage | Notes |
|---|---|---|
external_id | UUIDField(default=uuid4, unique=True, db_index=True) | The allergy's public ID. Returned as id |
created_date, modified_date | DateTimeField | Returned on read |
deleted | BooleanField(default=False, db_index=True) | Soft-delete flag. Normal queries hide soft-deleted rows |
created_by, updated_by | FK → users.User, on_delete=SET_NULL | created_by: the user who created the allergy. updated_by: the user who last created or updated it. Both returned on read |
history | JSONField(default=dict) | Updates do not write to it |
meta | JSONField(default=dict) | Every spec carries meta, default {} |
Enum values
The specs accept only these values on write. The second column is the label that the Care frontend shows for the value.
ClinicalStatusChoices values
| Value | Label in the Care frontend |
|---|---|
active | Active |
inactive | Inactive |
resolved | Resolved |
VerificationStatusChoices values
| Value | Label in the Care frontend |
|---|---|
unconfirmed | Unconfirmed |
presumed | Presumed |
confirmed | Confirmed |
refuted | Refuted |
entered_in_error | Entered in Error |
CategoryChoices values
| Value | Label in the Care frontend |
|---|---|
food | Food |
medication | Medication |
environment | Environment |
biologic | Biologic |
CriticalityChoices values
| Value | Label in the Care frontend |
|---|---|
low | Low |
high | High |
unable_to_assess | Unable to Assess |
AllergyIntoleranceTypeOptions values
| Value | Notes |
|---|---|
allergy | Spec default on create and on update |
intolerance |
The Care frontend never sends a type or an onset. Every allergy recorded from the Care UI has type allergy and no onset.
Nested JSON shapes
code: Coding
code is one Coding. Its code key is required; system, version and display are optional. Care rejects any other key.
Coding
{
"system": str | null, optional
"version": str | null, optional
"code": str, required
"display": str | null optional
}
Bound value set
| Name | Slug | Status | Default content |
|---|---|---|---|
| Allergy | system-allergy-code | active | SNOMED CT (http://snomed.info/sct) concepts that are is-a descendants of the eight concepts below |
| SNOMED CT code | Display name |
|---|---|
105590001 | Substance |
418038007 | Propensity to adverse reactions to substance |
267425008 | Lactose intolerance |
29736007 | Syndrome of carbohydrate intolerance |
340519003 | Lysine intolerance |
190753003 | Sucrose intolerance |
413427002 | Acquired fructose intolerance |
716186003 | No known allergy |
- On create, Care checks
codeagainst a value set with the slugsystem-allergy-code. A code outside it fails with "Code does not exist in the valueset". - If the value set is not in the database, the error is "Valueset does not exist in care, Resync valuesets".
- Value sets with the slug
system-allergy-codecan exist once instance-wide, and again per facility, per facility organization and per user. - On save, Care checks the code against the first value set it finds with that slug, without regard to facility.
- The allergen search (
POST /api/v1/valueset/expand_slug/) passes the current facility. It uses the user's preferred value set for that facility if there is one. - Otherwise the search chooses among that facility's value sets and the instance-wide ones with the slug.
- So the search and the save can use different value sets. When a facility has its own allergen list, the search offers its codes.
- Saving an allergen that is only in the facility's list fails with "Code does not exist in the valueset".
- It is a system value set. The sync command writes it to the database and leaves an existing copy unchanged unless told to overwrite it.
- The stored value set can be edited after the sync, so a deployment's content can differ from the default above.
105590001(Substance) is also the only root of Care's "Substance" value set, slugsystem-substance.- Every signed-in user can read the system value set.
onset: AllergyIntoleranceOnSetSpec
Onset is optional on create and defaults to {}. When an onset object is sent, its note is required. The update spec has no onset.
AllergyIntoleranceOnSetSpec
{
"onset_datetime": datetime, optional, default null
"onset_age": int, optional, default null
"onset_string": str, optional, default null
"note": str required, no default
}
Related models
AllergyIntolerance links to other models as follows. copied_from holds an internal ID, not a foreign key.
AllergyIntolerance
patient → FK emr.Patient (CASCADE)
encounter → FK emr.Encounter (CASCADE)
created_by → FK users.User (SET_NULL)
updated_by → FK users.User (SET_NULL)
copied_from → BigIntegerField: internal ID of the original AllergyIntolerance
Each create and each update through the API also writes a questionnaire response entry for the allergy. See "Methods & save behaviour" below.
Resource specs (API schema)
Every spec extends EMRResource. BaseAllergyIntoleranceSpec ties the specs to AllergyIntolerance and leaves patient and encounter out of the automatic field mapping.
The viewset uses the write spec for create, the update spec for update, and the read spec for reads. It sets no retrieve spec, so one allergy and the list return the same fields.
| Spec | Role | Fields |
|---|---|---|
BaseAllergyIntoleranceSpec | shared base | id (UUID4, optional), meta (dict, default {}) |
AllergyIntoleranceWriteSpec | write · create | clinical_status, verification_status, category, criticality, encounter, code, last_occurrence?, recorded_date?, onset?, allergy_intolerance_type?, note? |
AllergyIntoleranceUpdateSpec | write · update | clinical_status, verification_status, criticality, encounter, last_occurrence?, note?, allergy_intolerance_type? |
AllergyIntoleranceReadSpec | read · list and detail | id, clinical_status, verification_status, category, criticality, code, encounter, onset, last_occurrence, recorded_date, note, allergy_intolerance_type, created_by, updated_by, created_date, modified_date, meta |
AllergyIntoleranceOnSetSpec | nested · shape of onset | onset_datetime?, onset_age?, onset_string?, note |
Create: AllergyIntoleranceWriteSpec
The body of a create request, and of an upsert entry without id.
{
"encounter": UUID, required; the encounter must exist
"code": Coding, required; in value set system-allergy-code
"clinical_status": ClinicalStatusChoices, required
"verification_status": VerificationStatusChoices, required
"category": CategoryChoices, required
"criticality": CriticalityChoices, required
"last_occurrence": datetime | null, optional, default null
"recorded_date": datetime | null, optional, default null
"onset": AllergyIntoleranceOnSetSpec, optional, default {}
"allergy_intolerance_type": AllergyIntoleranceTypeOptions, optional, default "allergy"
"note": str | null optional, default null
}
- No spec has a
patientor acopied_fromfield. Care setspatientfrom the encounter on create, and setscopied_fromonly on copies. - Care does not write
idorexternal_idfrom the body. - A successful create answers HTTP 200, not 201, with the new allergy in the read format.
Update: AllergyIntoleranceUpdateSpec
The body of an update request, and of an upsert entry with id.
{
"encounter": UUID, required; the encounter must exist
"clinical_status": ClinicalStatusChoices, required
"verification_status": VerificationStatusChoices, required
"criticality": CriticalityChoices, required
"last_occurrence": datetime | null, optional, default null
"note": str | null, optional, default null
"allergy_intolerance_type": AllergyIntoleranceTypeOptions optional, default "allergy"
}
code,category,onsetandrecorded_dateare not in the update spec, so they cannot change after creation.- The update keeps
encounterfrom the request body, so every update names an encounter. - The allergy is linked to the encounter in the request. If that is a different encounter, Care keeps a copy on the old one.
- PUT and PATCH run the same update code. A successful update answers HTTP 200 with the updated allergy in the read format.
- Care does not write a value that equals the field's default. See "Methods & save behaviour" below.
Read: AllergyIntoleranceReadSpec
The list and the detail route return each allergy in this shape.
{
"id": UUID (the allergy's external_id),
"code": Coding,
"clinical_status": str,
"verification_status": str,
"category": str,
"criticality": str,
"encounter": UUID (the encounter's external_id),
"onset": AllergyIntoleranceOnSetSpec, default {},
"last_occurrence": datetime | null,
"recorded_date": datetime | null,
"note": str | null,
"allergy_intolerance_type": str,
"created_by": UserSpec | null,
"updated_by": UserSpec | null,
"created_date": datetime,
"modified_date": datetime,
"meta": object
}
- The read spec types the coded fields as
str. Reads build the spec withmodel_construct, which does not validate the values again. - Care adds
created_byandupdated_bywhen it reads the allergy. Each is a user object (UserSpec, read from the user cache), or null when not set. - A
UserSpeccarriesid,first_name,last_name,phone_number,prefix,suffix,last_login,profile_picture_url,gender,username,mfa_enabled,deleted,role_orgs,communicationandqualification. - The response has no
patientand nocopied_from.
Validation
encounter, on create and on update: no encounter with that ID gives "Encounter not found".code, on create: the code must be insystem-allergy-code. The errors are listed under "Bound value set".onset: when it is sent,onset.noteis required.clinical_status,verification_status,category,criticalityandallergy_intolerance_typeaccept only the enum values above.
A failed validation answers HTTP 400 with an errors list. Each error names the field path in loc and gives a msg.
HTTP 400
{
"errors": [
{ "loc": ["clinical_status"], "msg": "Input should be 'active', 'inactive' or 'resolved'" },
{ "loc": ["criticality"], "msg": "Field required" },
{ "loc": ["onset", "note"], "msg": "Field required" }
]
}
The three errors above are separate examples: a wrong clinical status, a missing criticality, and an onset without a note.
Methods & save behaviour
Create
- Care checks that the encounter exists, links the allergy to it, and sets
patientto that encounter's patient. - Care sets both
created_byandupdated_byto the user who sent the request. - Care writes a questionnaire response entry with submit type
CREATE. It holds the allergy's ID, the typeallergy_intolerance, and the user.
Update
- Care links the allergy to the encounter in the request.
- If that encounter differs from the stored one, Care first saves a copy of the allergy as it was. The copy stays on the old encounter, and its
copied_fromholds the original's internal ID. - If the new encounter already holds an earlier copy of this allergy, Care deletes that copy.
- Care sets
updated_byto the user who sent the request. - Care writes a questionnaire response entry with submit type
UPDATE. It holds the allergy's ID, the typeallergy_intolerance, and the user. - Care does not keep a field-by-field history of changes. The code marks this as a to-do.
Copies on earlier encounters
- Reads with patient-level access return only original allergies, never the copies left on old encounters.
- Without patient-level access, a list must name an encounter with
?encounter=. It then returns the allergies linked to that encounter, copies included. - On each save, the Care frontend's allergy form sends every saved allergy it loaded (up to 100) as an update, with the current encounter.
- So each save of the form links those allergies to the current encounter. Care keeps a copy of each one that was linked to another encounter.
- This is by design: users re-confirm the patient's allergy list at every encounter where they save the allergy form.
Care skips values equal to a default
- When Care saves a spec, it writes only the fields whose values differ from the field's default. It skips a value that equals the default.
- It writes only database fields that are not excluded and are not
idorexternal_id. Then it runs the spec's extra save step. - In the update spec, the defaults are
nullfornoteandlast_occurrence, andallergyforallergy_intolerance_type. An update that sends one of these values does not write that field. - Care accepts such an update, but keeps the stored value. Setting the type back to
allergy, ornoteorlast_occurrenceto empty, leaves the old value.
No delete
- Care has no action that deletes an allergy.
- The Care frontend marks an allergy as entered in error with an update that sets
verification_statustoentered_in_error. - The allergy lists of the Care frontend leave those allergies out with
exclude_verification_status=entered_in_error.
API integration notes
Routes
All routes are under /api/v1/, nested under the patient. {patient} is the patient's external ID. Every request needs a signed-in user.
| Action | Method and path | Request body | Success response |
|---|---|---|---|
| List | GET /api/v1/patient/{patient}/allergy_intolerance/ | None | count and results (read format) |
| View one | GET /api/v1/patient/{patient}/allergy_intolerance/{id}/ | None | The allergy (read format) |
| Create | POST /api/v1/patient/{patient}/allergy_intolerance/ | Write spec | HTTP 200, the new allergy (read format) |
| Update | PUT or PATCH /api/v1/patient/{patient}/allergy_intolerance/{id}/ | Update spec | HTTP 200, the updated allergy (read format) |
| Upsert | POST /api/v1/patient/{patient}/allergy_intolerance/upsert/ | datapoints: write or update spec, for each entry | HTTP 200, a list of the created or updated allergies, in request order |
| Delete | None | None | None |
{id}is the allergy'sid, as reads return it.- The list, view and update routes reach only allergies of the patient in the URL.
- In an upsert, Care looks up the allergy to update by its
idamong all allergies, not only those of the patient in the URL.
List filters, ordering and pages
| Query parameter | Effect |
|---|---|
encounter | Encounter external ID. Returns only allergies linked to that encounter |
clinical_status | Comma-separated values. Returns only these clinical statuses |
exclude_clinical_status | Comma-separated values. Leaves out these clinical statuses |
verification_status | Comma-separated values. Returns only these verification statuses |
exclude_verification_status | Comma-separated values. Leaves out these verification statuses |
name | Case-insensitive search for part of code.display |
limit | Records per page. Default 14, at most 200 |
offset | Records to skip |
ordering | created_date or modified_date. A leading minus, as in -created_date, reverses the order |
- Without
ordering, the list shows the most recently modified first. - Pages use
CareLimitOffsetPagination. The list answers withcount(the total) andresultsonly; it has nonextorpreviousfields.
Upsert
The upsert takes a batch of entries in datapoints.
POST /api/v1/patient/{patient}/allergy_intolerance/upsert/
{
"datapoints": [
{ "id": "<allergy id>", ...AllergyIntoleranceUpdateSpec fields },
{ ...AllergyIntoleranceWriteSpec fields }
]
}
- An entry with
idupdates that allergy. An entry withoutidcreates a new one. - An upsert takes 1 to 100 entries by default. The setting
MAX_DATAPOINTS_PER_UPSERTchanges the limit. - No entries gives "No datapoints provided". Too many gives "Too many datapoints provided".
- All or nothing: if any entry fails, Care saves none of them and returns 400 with a result for each entry.
- A permission refusal inside an upsert does not answer 403. The refused entry's result is
{"detail": "<message>"}, and the upsert answers 400. - On success, the upsert answers 200 with each created or updated allergy, in request order.
- Each entry needs the permission of its action: create or update, as in the table below.
Authorization
The table uses these terms:
- "Closed": the encounter status is
completed,cancelled,entered_in_errorordiscontinued. Any other status is open. - "A role on the patient": a role the user holds in an organization of an open encounter of the patient, or of its current location.
- A role on the patient can also come from an organization the patient belongs to, or be given directly on the patient. Closed encounters give no roles on the patient.
- "In the encounter": in one of the encounter's facility organizations, or in an organization of its current location.
- The encounter's facility organizations include the organizations it is assigned to, their parents, and the facility's root organization "Administration".
| Action | Permission (display name) | Held where | Refusal message |
|---|---|---|---|
| List, view one | "Can view clinical data about patients" | A role on the patient. Copies on old encounters are left out | None: Care tries the next row |
List, view one, with encounter and without the permission above | "Can Read encounter related clinical data" | In the encounter named in encounter. Returns only that encounter's allergies, copies included | "Permission denied to user" when neither row applies |
Create, and upsert entries without id | "Can Update a Patient's data" | A role on the patient in the URL | "You do not have permission to create" |
Update, and upsert entries with id | "Update Encounter related clinical data" | In the encounter named in the request body. Refused when that encounter is closed | "You do not have permission to update encounter" |
| Update (PUT, PATCH): finding the allergy | The read access of the first two rows | Without patient-level access, ?encounter= must name an encounter whose clinical data the user can read and to which the allergy is linked | As for list and view |
- Outside an upsert, a refusal answers HTTP 403 with the body
{"detail": "<message>"}, for example{"detail": "You do not have permission to create"}. - Create and upsert do not use the list's query, so neither needs the list's read access.
- When the setting
PATIENT_GLOBAL_EDIT_ACCESS_ENABLEDis on, "Can Update a Patient's data" in any facility organization or organization is enough to write to every patient. The setting is off by default. - The Care frontend's allergy form re-sends every saved allergy as an update. So once the patient has a saved allergy, a save from the form needs "Update Encounter related clinical data" in the current encounter. New rows also need "Can Update a Patient's data".
Default roles per permission:
| Permission (display name) | Slug | Default roles |
|---|---|---|
| Can view clinical data about patients | can_view_clinical_data | Staff, Doctor, Nurse, Facility Admin |
| Can Read encounter related clinical data | can_read_encounter_clinical_data | Doctor, Nurse, Facility Admin |
| Can Update a Patient's data | can_write_patient | Staff, Doctor, Nurse, Administrator, Facility Admin |
| Update Encounter related clinical data | can_write_encounter_clinical_data | Doctor, Nurse, Facility Admin |
These role lists are the defaults in code. Each permission sync resets the built-in roles to them.
Errors
- Validation errors return HTTP 400 with an
errorslist. See "Validation" above. - Permission refusals return HTTP 403 with a
detailmessage. Inside an upsert they return 400, with thedetailin the entry's result. - An allergy or patient that cannot be found returns HTTP 404 with type
object_not_found. - An encounter in the request body that does not exist fails validation: HTTP 400, "Encounter not found".
- Without patient-level access, an
encountervalue that names no encounter with readable clinical data gets HTTP 403, "Permission denied to user".
Other routes and features that carry allergies
- Batch requests:
POST /api/v1/batch_requests/runs its requests in one transaction. If any request fails, it returns 400 and saves none of them. - A batch holds at most 20 requests by default (setting
MAX_REQUESTS_PER_BATCH_REQUEST). - The batch endpoint checks only sign-in. Each request in it is authorized as if it were sent alone.
- The Care frontend saves the allergy form as one batch request that carries one allergy upsert.
- Questionnaires: the viewset is registered as the built-in questionnaire type
allergy_intolerance, titled "Allergy Intolerance", with the patient as subject. - A custom questionnaire cannot use
allergy_intoleranceas its slug. The error is "Slug cannot shadow internal question types". - When a questionnaire is submitted, the backend skips answer validation for structured questions such as the allergy question.
- Questionnaire responses:
/api/v1/patient/{patient}/questionnaire_responselists theCREATEandUPDATEentries. Theonly_unstructuredparameter leaves them out. - Reading those entries for an encounter needs patient-level clinical-data access, or clinical-data access on that encounter.
- Referrals: an allergy can be attached to a referral as a resource of type
allergy_intolerance, through the API only. The frontend's Add record picker offers only the types inREFERRAL_ATTACHABLE_RESOURCE_TYPES, which leave out allergies. The referral's Shared records list still shows an attached allergy, under Allergies. - To attach it, the user must be able to view the patient's clinical data, or the clinical data of the allergy's encounter.
- Reports: encounter report templates offer an "Allergy Intolerances" field that lists the allergies of the encounter.
- Its columns include Name, Clinical Status, Category, Verification Status, Criticality, Note, Occurrence and Allergy Intolerance Type.
Related
- Concept: Allergy intolerance
- Flow: Record an allergy
- Flow: Update an allergy
- Flow: Mark an allergy as entered in error
- Flow: View a patient's allergies
- Reference: Patient
- Reference: Encounter
- Reference: Questionnaire Response
- Reference: Value Set
- Reference: Permission
- Reference: Role
- Reference: Report & Templates
- Base: Base models & conventions
- Source (model): allergy_intolerance.py on GitHub
- Source (specs): resources/allergy_intolerance on GitHub
- Source (viewset): api/viewsets/allergy_intolerance.py on GitHub