accessing Subrecords

21 views
Skip to first unread message

Richard Mendes

unread,
May 11, 2018, 10:23:15 AM5/11/18
to OHC-OPAL
Dear Opal Users, 

I recently started looking into Opal and am struggling a bit with the data model setup and possibilities to access the data from the shell
I'm struggling in the part to access the data in the Demographic model class, in the admin panel we're setting up an agenda for a physician or
other medical staff but would like to have a filter based on birth date. 

Is there some more information available on how to access the data from the models in the terminal ? 

The setup stage is indeed relatively easy though the documentation online limited which makes it a very steep learning curve. 
Any additional documentation or examples would be greatly appreciated. 

best regards, 

Richard

David Miller

unread,
May 11, 2018, 10:32:57 AM5/11/18
to Richard Mendes, OHC-OPAL
Hi Richard, thanks for getting in touch.

It's always useful to know where the gaps in documentation are!

There are several ways to do it, but a couple that point you in the right direction:

```
> from yourapp.models import Demographics
> Demographics.objects.all()
Out[2]: <QuerySet [<Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, <Demographics: Demographics object>, '...(remaining elements truncated)...']>
> Demographics.objects.all()[0].name
'Clement Smith Jones'

> from opal.models import Patient
> frist_patient = Patient.objects.first()
> frist_patient.demographics_set.get()
 <Demographics: Demographics object>

> frist_patient.demographics_set.get().name
'Clement Smith Jones'
```
Worth noting that this isn't anything specific to Opal - a good general purpose Django orm / model tutorial is here: https://tutorial.djangogirls.org/en/django_orm/

Does that help or have I misunderstood your query?


Not sure I completely follow what you're trying to do with an admin panel for agenda :(

Do you have a sketch of the UI of the thing you're trying to build ? 

Best

David

--
You received this message because you are subscribed to the Google Groups "OHC-OPAL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--------------------
David Miller
Open Health Care

Richard Mendes

unread,
May 11, 2018, 10:49:05 AM5/11/18
to David Miller, OHC-OPAL
Hi David, 

Thanks for you quick reply, for now i'm just playing around to see what is possible with the opal setup. 

The first approach is the normal django model approach but this doesn't work with the opal setup unfortunately :-)
When there are multiple episodes listed it seems logical that a physician would like to see their agenda for the day, 
similar for the administration personal when there is a patient at the desk or calling they would like to make an appointment for a certain time. 

i would like to make an overview of the patients for a single day, multiple days, multiple physicians etc. 

As the demographic data is already in the opal model available for a certain patient i would like to access that data and filter on the birth_date. 
So if a patient comes to the desk the staff can locate the patient with a filter. 

At this point you need to find the patient based on id nr and from the patient object you can get the demographics, 
i would like to do it the other way around find the patient id based on certain demographic info. 


For me personally the gaps in the documentation is mainly the way how to access the data from all the models. 
So for instance the demographics is a class with a patient subrecord but there is no direct model involved to get the data

but how can i get the patient subrecord or the demographics listed 

The thought process i guess is different from what i've seen before in django projects but it's hard to understand based on the current documentation in my case. 

best regards, 

Richard

To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Fred Kingham

unread,
May 11, 2018, 10:57:02 AM5/11/18
to Richard Mendes, David Miller, OHC-OPAL
Hi Richard, 
 Sounds good and doable. When you say filter by date_of_birth do you mean

a) in the shell
b) in the admin
c) in the search box
d) in a patient_list view
e) all of the above!

I think you're looking at c, but just want to clarify.

 thanks,

  Fred

David Miller

unread,
May 11, 2018, 11:00:56 AM5/11/18
to Richard Mendes, OHC-OPAL
On Fri, 11 May 2018 at 15:49 Richard Mendes <mend...@gmail.com> wrote:
At this point you need to find the patient based on id nr and from the patient object you can get the demographics, 
i would like to do it the other way around find the patient id based on certain demographic info. 

In the shell this should be something like:
```
>>> from myapp.models import Demographics
>>> Demographics.objects.filter(date_of_birth='1974-05-26')[0].patient
<Patient: 60335139 | Gavin Sinfield>from myapp.models import Demographic
```

e.g. the patient attached to a Demographics instance is accessible via `demographic.patient` - though the (userland e.g. hospital) ID numbr is actually stored in the Demographics model itself...

Is that what you mean ? 

David

Richard Mendes

unread,
May 11, 2018, 11:24:36 AM5/11/18
to Fred Kingham, David Miller, OHC-OPAL
Hi Fred, David

I'm looking at b and c, i would like to setup a agenda based on the tools and filter in the admin panel. 
So it would become a custom filter field in the admin.py file that contains a date of birth field. 

@david, i'm currently only using the models from opal itself so it is from opal.models import Demographics. 
I'm not building a demographics model in my own app but extending the opal models.

there is no .objects for the demographics models at that point. am i missing something here ? 




David Miller

unread,
May 11, 2018, 11:40:24 AM5/11/18
to Richard Mendes, Fred Kingham, OHC-OPAL
The demographics model in Opal is a Django abstract model [0] - that means it won't get a database table.

Your application needs to subclass it.
This would have happened automatically if you ran `opal startproject foo`, in the file `./foo/models.py`
Unfortunately we've not gotten around to writing about this much in the docs despite noting it as a gap [1]. This may well be the push to make sure we do though :)

We take  this approach so that you can extend (by adding new fields) or alter the behaviour (by overriding methods) of these core models.

As a word to the wise, I would be very cautious about attempting to build an application by extending the Django Admin and letting users use that.

You are likely to find yourself boxed in by the combination of limitations and complexity relatively soon.

I say this as someone who has previously done exactly this and wished I hadn't ! :)

If I understand what you're after, If I was building an agenda with an Opal application I'd create an appointment type model: 

``
class Appointment(EpisodeSubrecord):
    date = DateField()
``

... and then use Opal patient lists [3] to create the various views - possibly with a group for today / this week / this month and with a custom comparator service to order by appointment date.

I'd have the get_queryset method of the patient list be something like: 

```
def get_queryset(self):
    week = datetime.date.today()+datetime.timedelta(days=7)
    Episode.objects.filter(appointment__date__lte=week)
```

...and then multiple lists for different times. And I'd probably put different defined times (today/week/month) in a `TabbedPatientListGroup`

HTH ! 

David

P.S. all code has been typed into this email and not a python shell / tested. Please be generous to my typos/syntax errors/forgetting to import things :)
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
--------------------
David Miller
Open Health Care

--
You received this message because you are subscribed to the Google Groups "OHC-OPAL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "OHC-OPAL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Richard Mendes

unread,
May 11, 2018, 12:30:58 PM5/11/18
to David Miller, Fred Kingham, OHC-OPAL
Hi David, 

Thanks a lot for the advice, 
i'll continue playing around with it the coming weeks. 

have a good weekend. 
regards, 

Richard

To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
--------------------
David Miller
Open Health Care

--
You received this message because you are subscribed to the Google Groups "OHC-OPAL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "OHC-OPAL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ohc-opal+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages