beda-software / fhir-py

FHIR Client for python
MIT License
168 stars 31 forks source link

Saving Device with reference to Patient not successful #87

Closed Henry6789 closed 2 years ago

Henry6789 commented 2 years ago

Hi all,

Just before execution of the last line (device.save) the variable 'device' contains the field 'subject' as specified, but immediately after execution of that line the subject field is gone. Anyone an idea why? The device is stored successfully, but without reference to a patient.

        # Get device, based on device serial number
        devices = client.resources('Device')
        try:
            device = await devices.search(identifier=sn).get()
        except ResourceNotFound:
            # Device doesn't exist in FHIR data store yet, create it
            # Create corresponding patient first
            patient = client.resource(
                "Patient"
            )
            await patient.save()
            # Then device, with reference to patient
            # TODO: Fix bug: Patient reference isn't stored
            device = client.resource(
                "Device",
                identifier={
                    "type": {
                        'coding': [
                            {
                                "system": 'https://hl7.org/fhir/ValueSet/identifier-type',
                                "code": 'SNO'
                            }
                        ]
                    },
                    "value": sn
                },
                subject={
                    "reference": patient.reference
                }
            )
            await device.save()
mkizesov commented 2 years ago

Hi @Henry6789 , what is the type of patient.reference? Could you try to use patient.to_reference() there?

mkizesov commented 2 years ago

Another way suggested by @ruscoder is to just use subject=patient:

device = client.resource(
                "Device",
                identifier={
                    "type": {
                        'coding': [
                            {
                                "system": 'https://hl7.org/fhir/ValueSet/identifier-type',
                                "code": 'SNO'
                            }
                        ]
                    },
                    "value": sn
                },
                subject=patient
            )
Henry6789 commented 2 years ago

@mkizesov Type of patient.reference is str, which is ok as far as I know. It's content is like "Patient/100". Saving observations with the same patient reference works, which means the issue is device related. The alternative by @ruscoder is very nice (will use this from now on), but the resulting reference is not saved either. Maybe the issue is caused by the HAPI FHIR server (version 4.1.0) that I'm talking to. Can you try if you can save a device with patient reference? Thanks in advance!

Henry6789 commented 2 years ago

Problem solved. The FHIR server that I'm using (mentioned above) supports FHIR standard R4.0.1. R4.0.1 uses the 'patient' field to refer to a patient, while later FHIR releases (like 4.6.0) use the 'subject' field. Sorry for raising an issue here, and thx for the support!

mkizesov commented 2 years ago

You are welcome. Also had a thought that it could be a server-related issue. So I close this issue.