Skip to main content
Version: 3.1

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:

Models​

ModelPurpose
QuestionnaireResponseOne filed set of answers to a Questionnaire for a subject (patient or encounter); also filed when a structured record is saved
FormSubmissionA 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​

FieldTypeNotes
questionnaireFK → QuestionnaireCASCADE, nullable. The questionnaire that was answered; null for a structured-record response
revisionIntegerFielddefault=1. The questionnaire's internal_revision at submit time
subject_idUUIDFieldThe subject the response is about
responsesJSONFielddefault=list. The raw answers, a list of { "question_id": UUID, ... } entries
cleaned_responseJSONFielddefault=dict. A cleaned copy of the answers, built at submit time
structured_responsesJSONFielddefault=dict. Set only for a structured-record response: names the record type, the record id and CREATE or UPDATE
structured_response_typeCharFieldNullable. The record type of a structured-record response; null for an answered questionnaire
patientFK → PatientCASCADE. The patient
encounterFK → EncounterCASCADE, nullable. Set for a questionnaire whose subject_type is encounter; null for a patient questionnaire
form_submissionFK → FormSubmissionCASCADE, nullable. The server draft this response came from
statusCharField(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​

FieldTypeNotes
questionnaireFK → QuestionnaireCASCADE. The questionnaire being submitted
patientFK → PatientCASCADE. The patient; taken from the encounter when one is given
encounterFK → EncounterCASCADE, nullable. Encounter context, if any
statusCharField(255)One of FormSubmissionStatusChoices (see enum)
response_dumpJSONFielddefault=dict. Written through FormSubmissionUpdateSpec and FormSubmissionWriteSpec

Enums​

QuestionnaireResponseStatusChoices values​

Defined in resources/questionnaire_response/spec.py; str values.

ValueMeaning
completedThe response is finalised (default)
entered_in_errorThe 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.

ValueMeaning
draftSaved but not finalised
submittedFinalised submission; cannot go back to draft
entered_in_errorRecorded 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 classRoleNotes
EMRQuestionnaireResponseBaseshared__model__ = QuestionnaireResponse
QuestionnaireResponseUpdatewrite · updateOnly status (QuestionnaireResponseStatusChoices, default completed), used to mark a response entered_in_error
QuestionnaireResponseReadSpecreadid, 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 classRoleNotes
BaseFormSubmissionSpecshared__model__ = FormSubmission; id (UUID4?)
FormSubmissionUpdateSpecwrite · updatestatus (FormSubmissionStatusChoices), response_dump (dict)
FormSubmissionWriteSpecwrite · createAdds 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
FormSubmissionReadSpecreadstatus, 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:

  1. 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").
  2. For a questionnaire whose subject_type is encounter, it needs an encounter of 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.
  3. For a questionnaire whose subject_type is patient, it refuses an encounter ("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.
  4. 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_when conditions hide ("... is not permitted by its enable_when conditions").
    • It skips structured questions, because their own record APIs save them.
  5. It creates one QuestionnaireResponse with responses and cleaned_response. For an encounter questionnaire, it also creates an Observation for each answer, linked to the response and the encounter. A patient questionnaire creates no observations.
  6. It sets revision to the questionnaire's current internal_revision.
  7. When form_submission is given, it links the response to that server draft. It refuses a draft that already has a response ("Form submission already has a response").
  8. 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_error cannot change: "Questionnaire Response cannot be edited".
  • After QUESTIONNAIRE_ERRORED_TIME_LIMIT_MINUTES from created_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 every Observation created from the response to entered_in_error.

API integration notes​

  • responses holds the answers as submitted, and cleaned_response holds a cleaned copy built at submit time. structured_responses is set only for structured-record responses.
  • Clients cannot set created_by or updated_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 sets REACT_ENABLE_QUESTIONNAIRE_DRAFT to true (default false).

Endpoints​

  • QuestionnaireResponseViewSet is 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 encounter only 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). Without limit, a list returns 14 items; the maximum limit is 200.

List filters​

ParameterMatches
encounterResponses of that encounter (external id)
subject_typeThe questionnaire's subject_type. Responses with no questionnaire never match
questionnaireThat questionnaire (by id) and its past revisions
questionnaire_slugThe questionnaire slug, exactly. A slug is unique only within its scope, so an instance and a facility questionnaire can share one
questionnaire_slugsAny slug in a comma-separated list
form_submissionResponses of that form submission (external id)
statuscompleted or entered_in_error
created_byResponses by that user (external id)
roleResponses whose author belongs to that role organization (external id)
only_unstructuredWhen present, only responses with no structured_response_type

Read access​

RequestNeeds
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.