Skip to main content
Version: 3.1

Questionnaire Response Template

Technical reference for the QuestionnaireResponseTemplate module in Care EMR. For what a template is and how people use it, see the Questionnaire Response Template concept.

A QuestionnaireResponseTemplate is a named, reusable bundle of medication requests and activity-definition service requests. The frontend applies it to a Medication Request or Service Request question while a user fills in an encounter questionnaire.

Source:

The Django model stores the template. The Pydantic resource specs define the template_data payload, its validation, and the read and write API schemas.

Models​

ModelPurpose
QuestionnaireResponseTemplateA named template of medication requests, activity-definition service requests and questionnaire answers, owned by a facility and shared with users and facility organizations

QuestionnaireResponseTemplate extends EMRBaseModel: external_id, created_date, modified_date, created_by, updated_by, soft delete through deleted, and history and meta JSON.

QuestionnaireResponseTemplate fields​

FieldTypeNotes
nameCharField(255)Display name in the template list
descriptionTextFielddefault=""
template_dataJSONFielddefault=dict. Shape defined by TemplateData (see shape)
facilityFK → FacilityCASCADE, nullable. Set on create; the update spec cannot change it. Decides where the write permission is checked and which facility organizations are valid. It does not limit who can read the template
questionnaireFK → QuestionnaireCASCADE, nullable, default=None. Set on create from a slug; the update spec cannot change it
facility_organizationsArrayField[int]default=list. Internal ids of the facility organizations the template is shared with. Written from external ids
usersArrayField[int]default=list. Internal ids of the users the template is shared with. Written from usernames
available_keysArrayField[CharField(255)]default=list. Server-maintained: the non-empty top-level keys of template_data, recomputed on every create and update. Do not set it

TemplateData shape​

template_data is an opaque JSONField on the model. The spec gives it this structure. Every key is optional, and available_keys records which keys a template carries.

KeyTypeNotes
medication_requestlist[MedicationRequestTemplateSpec], optionalMedication requests. The frontend writes this key from a Medication Request question
activity_definitionlist[ActivityDefinitionTemplateSpec], optionalService requests, each tied to an activity definition. The frontend writes this key from a Service Request question
questionnairelist[QuestionnaireAnswer], optionalPre-filled answers. The 3.1 template forms do not send this key
metadict, optionalFree-form

QuestionnaireAnswer​

FieldTypeNotes
question_idstr
answerdict
metadict

MedicationRequestTemplateSpec​

Extends MedicationRequestAbstractSpec; see Medication Request for the inherited fields. Adds one field:

FieldTypeNotes
requested_productstr, optionalA Product Knowledge slug. Must exist, else "Product knowledge not found"

ActivityDefinitionTemplateSpec​

FieldTypeNotes
slugstrAn activity definition slug. Must exist, else "Activity definition not found"
service_requestServiceRequestUpdateSpecThe service request details to apply with the activity definition

See Activity Definition and Service Request for the embedded specs.

Resource specs (API schema)​

All specs extend EMRResource.

Spec classRoleNotes
QuestionnaireResponseTemplateBaseSpecshared__model__ = QuestionnaireResponseTemplate; id (UUID4, optional), template_data (TemplateData), name, description (default "")
QuestionnaireResponseTemplateCreateSpecwrite · createAdds questionnaire (slug, str, optional), facility (UUID4, optional), users (list[str], usernames, required) and facility_organizations (list[UUID4], required). Requires facility when facility_organizations is not empty. Resolves the questionnaire by slug and the facility by external id; computes available_keys
QuestionnaireResponseTemplateUpdateSpecwrite · updatename, description, template_data, users and facility_organizations (both required). No facility or questionnaire. Recomputes available_keys
QuestionnaireResponseTemplateReadSpecread · listAdds created_date and modified_date; id is the external id
QuestionnaireResponseTemplateRetrieveSpecread · detailExpands users (UserSpec from cache) and facility_organizations (FacilityOrganizationReadSpec; ids that no longer resolve are skipped); adds created_by and updated_by

Validation and server-maintained behaviour​

  • validate_facility (create, model_validator(after)) rejects facility_organizations without a facility: "Facility is required if facility organizations are provided".
  • validate_data resolves each username in users and each external id in facility_organizations. Facility organizations must belong to the template's facility. An unknown value returns 404. This runs before the permission check.
  • requested_product and the activity-definition slug are checked to exist against ProductKnowledge and ActivityDefinition.
  • On create, perform_extra_deserialization resolves questionnaire with get_object_or_404(Questionnaire, slug=…), after the permission check.
  • Past revisions of a questionnaire keep its slug, and other facilities can reuse a slug, so that lookup can match more than one row. The resulting MultipleObjectsReturned is not handled, and the create fails with HTTP 500. At runtime this happens once the questionnaire has one past revision.
  • available_keys is derived on every create and update from the template_data keys whose value is not empty. Clients do not set it.
  • Update writes users and facility_organizations from the request every time, because they have no default and exclude_defaults keeps them. A request with users: [<caller>] and facility_organizations: [] replaces the template's sharing. The frontend's "Add to template" on an existing template sends exactly this.

API integration notes​

Endpoints​

The viewset is registered at the top level, not nested under a facility. Lookup is by external_id.

MethodPathAction
GET/api/v1/questionnaire_response_template/List (QuestionnaireResponseTemplateReadSpec)
POST/api/v1/questionnaire_response_template/Create
GET/api/v1/questionnaire_response_template/<id>/Retrieve (QuestionnaireResponseTemplateRetrieveSpec)
PUT/api/v1/questionnaire_response_template/<id>/Update (PATCH routes to the same handler)
DELETE/api/v1/questionnaire_response_template/<id>/Soft delete (deleted = True); returns 204
POST/api/v1/questionnaire_response_template/upsert/Create or update a batch given as datapoints

Filters and ordering​

  • name: contains, case-insensitive.
  • questionnaire: exact questionnaire slug.
  • key_filter: available_keys contains the value. The frontend sends medication_request or activity_definition.
  • facility: facility external id.
  • Ordering: created_date, modified_date.

Visibility​

  • List, retrieve, update and delete see only templates that the caller created, templates whose users include the caller, and templates shared with a facility organization in which the caller holds a role. Only direct membership counts, not parent or child organizations.
  • No permission is checked to list or retrieve, and facility plays no part in visibility. A template outside these rules returns 404, even to a caller with the write permission.
  • The frontend lists with facility, questionnaire and key_filter, so a user sees only the templates of the current facility and questionnaire for that kind of question.
  • Deleted templates are left out of every query.

Authorization​

  • Create, update and destroy call can_write_questionnaire_response_template(user, facility). There is no creator check. A refusal is 403 "Access Denied to Questionnaire Response Template".
  • With a facility, the check needs "Can Create/Update Questionnaire Response Templates" in any facility organization of that facility. Without a facility, it needs the permission in any organization.
  • By default, the Doctor, Nurse, Staff, Pharmacist, Administrator, Facility Admin and Volunteer roles hold the permission.
  • The frontend's department picker lists facility organizations, which needs "Can View Facility Organizations" in the facility.

Other notes​

  • facility, facility_organizations and users control sharing. Set facility to share with facility organizations.
  • Read available_keys, or filter with key_filter, to learn which sections a template carries without parsing template_data.