Questionnaire Response
A QuestionnaireResponse is the set of answers a user files for a questionnaire, about a patient or about one of the patient's encounters. A FormSubmission is the server draft that carries those answers through a draft-to-submitted lifecycle. You touch these when you read back what was answered or submit new answers.
For the plain-language view, read the Questionnaire Response concept.
The Django models are the storage; the Pydantic resource specs define the submit payload, answer structure, status enums, and read/write API schemas.
Source:
- Model:
care/emr/models/questionnaire.py - Specs:
resources/questionnaire_response/spec.py·resources/form_submission/spec.py
Models
| Model | Purpose |
|---|---|
QuestionnaireResponse | One filed set of answers to a Questionnaire for a subject (patient or encounter); also filed when a structured record is saved |
FormSubmission | A server draft (draft → submitted) for a questionnaire, a patient and an optional encounter, with a JSON response_dump |
Both extend EMRBaseModel, the shared Care EMR base model.
QuestionnaireResponse fields
| Field | Type | Notes |
|---|---|---|
questionnaire | FK → Questionnaire | CASCADE, nullable. The questionnaire that was answered; null for a structured-record response |
revision | IntegerField | default=1. The questionnaire's internal_revision at submit time |
subject_id | UUIDField | The subject the response is about |
responses | JSONField | default=list. The raw answers, a list of { "question_id": UUID, ... } entries |
cleaned_response | JSONField | default=dict. A cleaned copy of the answers, built at submit time |
structured_responses | JSONField | default=dict. Set only for a structured-record response: names the record type, the record id and CREATE or UPDATE |
structured_response_type | CharField | Nullable. The record type of a structured-record response; null for an answered questionnaire |
patient | FK → Patient | CASCADE. The patient |
encounter | FK → Encounter | CASCADE, nullable. Set for a questionnaire whose subject_type is encounter; null for a patient questionnaire |
form_submission | FK → FormSubmission | CASCADE, nullable. The server draft this response came from |
status | CharField(255) | default="completed". One of QuestionnaireResponseStatusChoices (see enum) |
resolved_questionnaire and render_responses()
A response keeps the questionnaire revision it answered. At submit time, revision stores the questionnaire's current internal_revision.
resolved_questionnaire returns questionnaire when revision equals its current internal_revision. Otherwise it loads the past revision with that revision number. It returns None when no questionnaire is linked.
render_responses() joins each entry in responses with its question from the resolved revision's get_questions_by_id(). It returns a list of { "answer": <response>, "question": <question> } and skips answers whose question is not in that revision. It returns [] when responses is empty or no questionnaire is linked.
The read spec serializes the resolved revision as questionnaire, so a client can show each answer with the question as it was asked.
Structured-record responses
Saving an allergy, symptom, diagnosis, procedure, medication request, medication statement or medication administration record also files a QuestionnaireResponse, on create and on update. It has no questionnaire; structured_response_type names the record type, and structured_responses is { <record type>: { "submit_type": "CREATE" | "UPDATE", "id": <record id> } }. No response list in the web client shows these responses, because each list filters on subject_type.
FormSubmission fields
| Field | Type | Notes |
|---|---|---|
questionnaire | FK → Questionnaire | CASCADE. The questionnaire being submitted |
patient | FK → Patient | CASCADE. The patient; taken from the encounter when one is given |
encounter | FK → Encounter | CASCADE, nullable. Encounter context, if any |
status | CharField(255) | One of FormSubmissionStatusChoices (see enum) |
response_dump | JSONField | default=dict. Written through FormSubmissionUpdateSpec and FormSubmissionWriteSpec |
Enums
QuestionnaireResponseStatusChoices values
Defined in resources/questionnaire_response/spec.py; str values.
| Value | Meaning |
|---|---|
completed | The response is finalised (default) |
entered_in_error | The response was recorded in error. It cannot change again, and the observations created from it are marked entered_in_error too |
FormSubmissionStatusChoices values
Defined in resources/form_submission/spec.py; str values.
| Value | Meaning |
|---|---|
draft | Saved but not finalised |
submitted | Finalised submission; cannot go back to draft |
entered_in_error | Recorded in error; cannot change ("Form submission is already entered in error") |
Submit payload (nested shapes)
Answers go through the questionnaire submit action at /api/v1/questionnaire/{id}/submit/, whose body is QuestionnaireSubmitRequest. Repeating groups nest in sub_results, and each answer can carry several values:
QuestionnaireSubmitRequest {
resource_id: UUID4 # stored as the response's subject_id
patient: UUID4 (required)
encounter: UUID4 | None
form_submission: UUID4 | None
results: list[QuestionnaireSubmitResult]
}
QuestionnaireSubmitResult {
question_id: UUID4 | UUID5 (required)
body_site: Coding | None
method: Coding | None
taken_at: datetime | None
values: list[QuestionnaireSubmitResultValue]
note: str | None
sub_results: list[list[QuestionnaireSubmitResult]] # nested, for repeating groups
}
QuestionnaireSubmitResultValue {
value: str | None
unit: Coding | None # for Quantity answers
coding: Coding | None # for coded answers
}
Resource specs (API schema)
The QuestionnaireResponse and FormSubmission specs extend EMRResource (resources/base.py). QuestionnaireSubmitRequest and its nested classes are plain pydantic BaseModel classes.
QuestionnaireResponse (resources/questionnaire_response/spec.py)
There is no generic create spec. New responses come from the questionnaire submit action (QuestionnaireSubmitRequest) or from a structured-record save. The update spec carries status only, so the one direct write is marking a response entered_in_error.
| Spec class | Role | Notes |
|---|---|---|
EMRQuestionnaireResponseBase | shared | __model__ = QuestionnaireResponse |
QuestionnaireResponseUpdate | write · update | Only status (QuestionnaireResponseStatusChoices, default completed), used to mark a response entered_in_error |
QuestionnaireResponseReadSpec | read | id, status, questionnaire (nested QuestionnaireReadSpec of the resolved revision), questionnaire_latest_revision_id (external id of the current questionnaire), subject_id, responses, cleaned_response, encounter (external id or null), structured_responses, structured_response_type, created_by/updated_by (UserSpec), created_date/modified_date |
When the questionnaire has changed since submission, questionnaire is the past revision the response answered, and questionnaire_latest_revision_id points to the current questionnaire.
FormSubmission (resources/form_submission/spec.py)
| Spec class | Role | Notes |
|---|---|---|
BaseFormSubmissionSpec | shared | __model__ = FormSubmission; id (UUID4?) |
FormSubmissionUpdateSpec | write · update | status (FormSubmissionStatusChoices), response_dump (dict) |
FormSubmissionWriteSpec | write · create | Adds questionnaire (UUID4), patient (UUID4), encounter (UUID4?). perform_extra_deserialization resolves the questionnaire and the patient/encounter by external id and refuses a referral questionnaire ("Referral questionnaires must be submitted through a referral"); when an encounter is given, the patient is taken from it |
FormSubmissionReadSpec | read | status, response_dump, created_date/modified_date, created_by/updated_by (UserSpec?) |
Each call runs the encounter submit check (can_submit_encounter_questionnaire_obj) or the patient submit check (can_submit_questionnaire_patient_obj). Create checks the body's encounter when one is given, else its patient. Retrieve and update check the submission's encounter when it has one, else its patient. List checks the encounter or patient query parameter.
A list call must name an encounter or a patient ("Patient or encounter is required").
On update, a submitted submission cannot go back to draft ("Form submission cannot be set to draft after submission"), and an entered_in_error submission cannot change ("Form submission is already entered in error").
Methods & save behaviour
Submitting answers
The submit action runs these steps in order:
- It refuses a referral questionnaire ("Referral questionnaires must be submitted through a referral") and a past revision ("This Questionniare is a past revision, please submit to the latest revision").
- For a questionnaire whose
subject_typeisencounter, it needs anencounterof that patient ("Encounter is required for this questionnaire"). It refuses a facility questionnaire for an encounter in another facility ("Encounter facility does not match questionnaire facility"). Then it runs the encounter submit check (can_submit_encounter_questionnaire_obj), or refuses with "Permission Denied to submit encounter questionnaire".- The encounter submit check refuses a closed encounter (
completed,cancelled,entered_in_error,discontinued). Otherwise it needs "Can submit questionnaire about patient encounters" in the encounter's departments or its current location's departments.
- The encounter submit check refuses a closed encounter (
- For a questionnaire whose
subject_typeispatient, it refuses anencounter("Encounter cannot be provided for a patient questionnaire"). Then it runs the patient submit check (can_submit_questionnaire_patient_obj), or refuses with "Permission Denied to submit patient questionnaire".- The patient submit check needs "Can submit questionnaire about patients" among the user's roles on the patient.
- It validates the answers.
- It refuses an inactive questionnaire ("Questionnaire is inactive") and a submission with no answers ("Empty Questionnaire cannot be submitted").
- It refuses a required question that is left out ("Question not answered") or sent with no value ("No value provided for question").
- It refuses an answer to a question that its
enable_whenconditions hide ("... is not permitted by its enable_when conditions"). - It skips structured questions, because their own record APIs save them.
- It creates one
QuestionnaireResponsewithresponsesandcleaned_response. For an encounter questionnaire, it also creates anObservationfor each answer, linked to the response and the encounter. A patient questionnaire creates no observations. - It sets
revisionto the questionnaire's currentinternal_revision. - When
form_submissionis given, it links the response to that server draft. It refuses a draft that already has a response ("Form submission already has a response"). - It runs the questionnaire's
actions, if any, in the same transaction.
The submit action loads the questionnaire with get_object, which runs no retrieve authorization. So the action does not check read access to the questionnaire.
Marking a response entered in error
QuestionnaireResponseUpdate carries only status, so no client can edit the answers after submission. The update runs these checks:
- A response that is already
entered_in_errorcannot change: "Questionnaire Response cannot be edited". - After
QUESTIONNAIRE_ERRORED_TIME_LIMIT_MINUTESfromcreated_date, the API refuses with the same message. The limit is 120 minutes unless the deployment sets the variable. - A response with an encounter needs the encounter submit check (
can_submit_encounter_questionnaire_obj), so a closed encounter refuses the change. - A response with no encounter needs the patient submit check (
can_submit_questionnaire_patient_obj). - When the status changes to
entered_in_error, the update also sets everyObservationcreated from the response toentered_in_error.
API integration notes
responsesholds the answers as submitted, andcleaned_responseholds a cleaned copy built at submit time.structured_responsesis set only for structured-record responses.- Clients cannot set
created_byorupdated_by: neither the submit body nor the update spec carries them. - In the web client, "Save as Draft" saves a server draft (
FormSubmission). The button appears only when the deployment setsREACT_ENABLE_QUESTIONNAIRE_DRAFTtotrue(defaultfalse).
Endpoints
QuestionnaireResponseViewSetis registered only under a patient, at/api/v1/patient/{patient_external_id}/questionnaire_response/. It offers list, retrieve and update; there is no create endpoint.- The questionnaire submit action at
/api/v1/questionnaire/{id}/submit/creates responses, and so does a structured-record save. - The web client saves a form session as one batch request. Each questionnaire's answers go to its submit action, with
encounteronly for an encounter questionnaire. Structured answers go to their own record APIs in the same batch. - The batch endpoint runs all sub-requests in one transaction. A sub-request with an error rolls back the whole batch.
- Lists are ordered newest first (
-created_date). Withoutlimit, a list returns 14 items; the maximumlimitis 200.
List filters
| Parameter | Matches |
|---|---|
encounter | Responses of that encounter (external id) |
subject_type | The questionnaire's subject_type. Responses with no questionnaire never match |
questionnaire | That questionnaire (by id) and its past revisions |
questionnaire_slug | The questionnaire slug, exactly. A slug is unique only within its scope, so an instance and a facility questionnaire can share one |
questionnaire_slugs | Any slug in a comma-separated list |
form_submission | Responses of that form submission (external id) |
status | completed or entered_in_error |
created_by | Responses by that user (external id) |
role | Responses whose author belongs to that role organization (external id) |
only_unstructured | When present, only responses with no structured_response_type |
Read access
| Request | Needs |
|---|---|
List with encounter; retrieve or update of a response that has an encounter | "Can view clinical data about patients" on the patient (can_view_clinical_data) or "Can Read encounter related clinical data" on the encounter (can_view_encounter_clinical_data) |
List without encounter (all of a patient's responses); retrieve or update of a response with no encounter | "Can view questionnaire responses on patient" on the patient (can_view_patient_questionnaire_responses) |
Without the permission, the API answers "You do not have permission to view questionnaire responses".
Patient permissions come from the user's roles on the patient. These roles come from the departments of the patient's encounters that are not closed (and their current locations), the organizations the patient belongs to, and a direct patient-user link.
The encounter permission is checked in the encounter's departments and, when the encounter has a current location, in that location's departments.
Related
- Concept: Questionnaire Response
- Reference: Questionnaire — the form definition being answered
- Reference: Questionnaire Response Template — reusable pre-fill templates
- Reference: Encounter
- Reference: Observation — created from the answers to an encounter questionnaire
- Concept: Patient
- Source: questionnaire.py on GitHub