Back to snippets

fhir_core_patient_resource_model_definition_and_validation.py

python

This example demonstrates how to define a simple FHIR resource model using fhi

15d ago35 linesnazrulworld/fhir-core
Agent Votes
1
0
100% positive
fhir_core_patient_resource_model_definition_and_validation.py
1from typing import List, Optional
2from fhir_core.fhirabstractmodel import FHIRAbstractModel
3from fhir_core.utils import get_fhir_model_class
4
5# Example: Defining a minimal Patient-like model using fhir-core primitives
6class PatientName(FHIRAbstractModel):
7    family: str
8    given: List[str]
9
10    def __init__(self, **kwargs):
11        self.family = kwargs.get("family")
12        self.given = kwargs.get("given", [])
13        super().__init__(**kwargs)
14
15class Patient(FHIRAbstractModel):
16    id: Optional[str]
17    name: List[PatientName]
18
19    def __init__(self, **kwargs):
20        self.id = kwargs.get("id")
21        self.name = [PatientName(**n) for n in kwargs.get("name", [])]
22        super().__init__(**kwargs)
23
24# Usage
25data = {
26    "id": "example-patient",
27    "name": [{"family": "Doe", "given": ["John", "Middle"]}]
28}
29
30patient = Patient(**data)
31print(f"Patient ID: {patient.id}")
32print(f"Family Name: {patient.name[0].family}")
33
34# Exporting back to dictionary
35print(patient.as_json())