Base FHIR Models
These classes provide base for constructing Fhircraft-compatible Pydantic FHIR models.
FHIRBaseModel
Path: fhircraft.fhir.resources.base.FHIRBaseModel
Bases: BaseModel, FHIRPathMixin
Base class for representation of FHIR resources as Pydantic objects.
Expands the Pydantic BaseModel class with FHIR-specific methods.
Methods:
| Name | Description |
|---|---|
model_post_init |
Initialize model and set up parent tracking. |
model_dump_xml |
Serialize the FHIR resource to XML format according to FHIR specification. |
model_construct |
Constructs a model without running validation, with an option to set default values for fields that have them defined. |
model_validate |
Override model_validate to provide default kwargs for FHIR resources. |
model_validate_json |
Override model_validate_json to provide default kwargs for FHIR resources. |
model_validate_xml |
Deserialize FHIR XML data into a model instance. |
model_copy |
Override model_copy to reset parent context on copied instance. |
model_construct_with_slices |
Constructs a model with sliced elements by creating empty slice instances based on the specified number of slice copies. |
get_sliced_elements |
Get the sliced elements from the model fields and their extension fields. |
clean_unusued_slice_instances |
Cleans up unused or incomplete slice instances within the given FHIR resource by iterating through the |
model_post_init
model_post_init(context: Any) -> None
model_dump_xml
model_dump_xml(*, indent: int | None = None, ensure_ascii: bool = True, include: IncEx | None = None, exclude: IncEx | None = None, exclude_unset: bool = False, exclude_none: bool = False, exclude_defaults: bool = False) -> str
Serialize the FHIR resource to XML format according to FHIR specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int | None
|
Indentation to use in the XML output. If None is passed, the output will be compact. |
None
|
ensure_ascii
|
bool
|
Whether to escape non-ASCII characters. |
True
|
include
|
IncEx | None
|
Fields to include in the output |
None
|
exclude
|
IncEx | None
|
Fields to exclude from the output |
None
|
exclude_unset
|
bool
|
Whether to exclude fields that were not explicitly set |
False
|
exclude_none
|
bool
|
Whether to exclude fields with None values |
False
|
exclude_defaults
|
bool
|
Whether to exclude fields with default values |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A string containing the XML representation of the FHIR resource |
Source code in fhircraft/fhir/resources/base.py
model_construct
classmethod
model_construct(set_defaults=True, *args, **kwargs) -> Self
Constructs a model without running validation, with an option to set default values for fields that have them defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
set_defaults
|
bool
|
Optional, if |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
instance |
Self
|
An instance of the model. |
Source code in fhircraft/fhir/resources/base.py
model_validate
classmethod
model_validate(obj, *, strict=None, from_attributes=None, context=None) -> Self
Override model_validate to provide default kwargs for FHIR resources.
Source code in fhircraft/fhir/resources/base.py
model_validate_json
classmethod
model_validate_json(json_data: str, *, strict: bool = False, context: Any = None, extra: ExtraValues | None = None) -> Self
Override model_validate_json to provide default kwargs for FHIR resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_data
|
str
|
JSON string to deserialize |
required |
strict
|
bool
|
Whether to validate strictly |
False
|
context
|
Any
|
Additional context for validation |
None
|
extra
|
ExtraValues | None
|
Extra parameters |
None
|
Source code in fhircraft/fhir/resources/base.py
model_validate_xml
classmethod
Deserialize FHIR XML data into a model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_data
|
str
|
XML string to deserialize |
required |
strict
|
bool
|
Whether to validate strictly |
None
|
context
|
Any
|
Additional context for validation |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
An instance of the model populated from the XML data |
Source code in fhircraft/fhir/resources/base.py
model_copy
Override model_copy to reset parent context on copied instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update
|
dict[str, Any] | None
|
Optional dict of field updates to apply to the copy |
None
|
deep
|
bool
|
Whether to perform a deep copy |
False
|
Returns:
| Type | Description |
|---|---|
Self
|
A copied instance with reset parent context |
Source code in fhircraft/fhir/resources/base.py
model_construct_with_slices
classmethod
Constructs a model with sliced elements by creating empty slice instances based on the specified number of slice copies. The method iterates over the sliced elements of the class, generates slice resources, and sets them in the resource collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slice_copies
|
int
|
Optional, an integer specifying the number of copies for each slice (default is 9). |
9
|
Returns:
| Name | Type | Description |
|---|---|---|
instance |
Self
|
An instance of the model with the sliced elements constructed. |
Source code in fhircraft/fhir/resources/base.py
get_sliced_elements
classmethod
get_sliced_elements() -> dict[str, list[type[FHIRSliceModel]]]
Get the sliced elements from the model fields and their extension fields.
Sliced elements are filtered based on being instances of FHIRSliceModel.
Returns:
| Name | Type | Description |
|---|---|---|
slices |
dict
|
A dictionary with field names as keys and corresponding sliced elements as values. |
Source code in fhircraft/fhir/resources/base.py
clean_unusued_slice_instances
classmethod
Cleans up unused or incomplete slice instances within the given FHIR resource by iterating through the sliced elements of the class, identifying valid elements, and updating the resource with only the valid slices.
Source code in fhircraft/fhir/resources/base.py
FHIRList
Path: fhircraft.fhir.resources.base.FHIRList
Bases: list
Custom list wrapper that maintains parent context on mutations.
This list automatically propagates _parent, _root_resource, _resource, and _index context to FHIRBaseModel items when they are added via append, extend, insert, or setitem.
Methods:
| Name | Description |
|---|---|
append |
Append item and propagate context. |
extend |
Extend list and propagate context to new items. |
insert |
Insert item and propagate context. |
Source code in fhircraft/fhir/resources/base.py
append
Append item and propagate context.
Source code in fhircraft/fhir/resources/base.py
extend
Extend list and propagate context to new items.
Source code in fhircraft/fhir/resources/base.py
insert
Insert item and propagate context.
Source code in fhircraft/fhir/resources/base.py
FHIRSliceModel
Path: fhircraft.fhir.resources.base.FHIRSliceModel
Bases: FHIRBaseModel
Base class for representation of FHIR profiled slices as Pydantic objects.
Expands the FHIRBaseModel class with slice-specific methods.
Attributes:
| Name | Type | Description |
|---|---|---|
is_FHIR_complete |
Validates if the FHIR model is complete by attempting to validate the model dump. |
|
has_been_modified |
Checks if the FHIRSliceModel instance has been modified by comparing it with a new instance constructed with slices. |
is_FHIR_complete
property
Validates if the FHIR model is complete by attempting to validate the model dump.
Returns True if the model is complete, False otherwise.