0

I want to append some relevant data to FHIR lets say patient or condition object for further processing. I tried to extend the FHIR patient class in python. Getting the following error. Could somebody give me some helpful pointers here? Thanks!

import fhir.resources.patient as pt

patient = pt.Patient.parse_file('patient.json')


class Patient2(pt.Patient):
    def __init__(self,validDia):
        self.validDia=validDia


newPatient = Patient2(1)
print(newPatient)


---------------------
  File "pydantic\main.py", line 357, in pydantic.main.BaseModel.__setattr__
ValueError: "Patient2" object has no field "validDia" ```
Rakesh
  • 104
  • 1
  • 11
  • 1
    When it comes to FHIR, you need to ask yourself "Why am I extending it??" And it needs to be a much better reason than "so I can jam some extra info on an existing resource because that's easier than using the CORRECT (other) fhir-resource". If you're just gonna 'custom-extend' an existing FHIR resource...you are missing the point of FHIR and interoperability. – granadaCoder Aug 23 '22 at 19:32
  • 1
    You tagged this question with hapi-fhir. (which is java based). I'll give you this link..but again "knowing how to do something" is different from "should I be doing this?" https://hapifhir.io/hapi-fhir/docs/model/custom_structures.html – granadaCoder Aug 23 '22 at 19:45

1 Answers1

1

If you want to extend your model you have to use Pydantic models.

For patient.json file i used data from fhir dock

Code example:

import json
import fhir.resources.patient as pt


class Patient2(pt.Patient):
    validDia: int


with open("patient.json", "r") as file:
    json_obj = json.load(file)


json_obj.update({"validDia": 1})


newPatient = Patient2.parse_obj(json_obj)
print(newPatient)
print(newPatient.validDia)

And the output:

>>> resource_type='Patient' fhir_comments=None id='p001' ...cut out... validDia=1
>>> 1
JacekK
  • 623
  • 6
  • 11