checking for field choices in jinja2

45 views
Skip to first unread message

chandni...@gmail.com

unread,
Jul 6, 2020, 2:58:33 AM7/6/20
to OHC-OPAL
Hi 

I am trying to access the model field choices inside the jinja2 template, basically I am trying to do something similar like this - 

{% for field in form %} 
 {% for x, y in field.field.choices %}
 {{x}}
 {{y}}
 {% endfor %}
 {% endfor %}

inside my current record_form.jina2 file I updated the code to this-
{% if f['type'] == 'string' and f.choices %}{{ '{' + '% radio' }}

===>   just to check if it sees the choices in my test model. this is not working. 
I tried searching but didnt find a solution that worked, also I have not used jinja anytime before. 

Can someone hint if I am going in the right direction or whats the way to access a field with choices. 

Thanks. 
Chandni

David Miller

unread,
Jul 6, 2020, 4:09:32 AM7/6/20
to chandni...@gmail.com, OHC-OPAL
Hi Chandni,

I wonder if you tried rendering the variable {{ f }} into the template to see what it consisted of?

You'll notice that the f in {% for f in fields %} is a dictionary, which doesn't have a choices attribute or key - which means of course that your sample code will always evaluate to False.

Another way to debug what's happening would be to trace the call stack that's rendering that template - you'll find that each datastructure in the list {{ fields }} is generated by the method opal.models.SerialisableFields.build_schema_for_field_name 

Reading that code would also tell you what to look at in your check to see that there are some choices.

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.
To view this discussion on the web, visit https://groups.google.com/d/msgid/ohc-opal/d3671e4c-f6e3-47e1-9c93-9a2ad5008f3dn%40googlegroups.com.


--
David Miller
Open Health Care

Fred Kingham

unread,
Jul 6, 2020, 5:20:51 AM7/6/20
to David Miller, chandni...@gmail.com, OHC-OPAL
Rendering {{ f }} like David is a good call.

I'm assuming you have an opal basic app running (ie the app you create with opal startproject).

If you run it, and go to http://127.0.0.1:8000/api/v0.1/record/ (assuming you are running on 127.0.0.1:8000)

You see a list of subrecords schemas, each containing a dictionary. E.g. 

Screen Shot 2020-07-06 at 10.15.08.png

What you have in the jinja context is the information for a specific subrecord, i.e. everything underneath demographics in the above.

When there is a choice field, the dictionary in the fields array looks like
Screen Shot 2020-07-06 at 10.19.27.png

 Hope that helps and does not just make everything more confusing!

 Fred






--
Fred Kingham
Open Health Care

chandni...@gmail.com

unread,
Jul 6, 2020, 7:58:54 PM7/6/20
to OHC-OPAL
Thanks David and Fred,

I actually did not debug that way, I knew its a dictionary with keys to lookup for choices but I rather started to search for jinja tutorials to find out how choices are stored and thats where I got lost. 

Now I know the structure better than before by using {{ f }} and Fred's screenshot helped me a lot in verifying thereafter. 

Can you please verify if this is how it should look like:

TODOItem Model with choices field


Form generated. 



Code changes inside record_form.jinja2
--
--

{% for f in fields %}{% if f['name'] != 'consistency_token' %}
{% if f['type'] == 'string' and f['enum'] %}

{% for f_choices in f['enum'] %}
{{ '{' + '% radio' }} field="{{ f["model"] }}.{{ f["name"] }}" label="{{ f_choices }}" option="{{ f_choices }}" {{'%' +'}' }}
{% endfor %}

{% elif f['type'] == 'string' %}{{ '{' + '% input' }} field="{{ f["model"] }}.{{ f["name"] }}" {{'%' +'}' }}
{% endif %}
--
--


Thanks

David Miller

unread,
Jul 7, 2020, 4:13:44 AM7/7/20
to chandni...@gmail.com, OHC-OPAL
I can't see the result rendered, but I wouldn't imagine you need to iterate through the enum fields - that's done by the templatetag {% radio %} itself...

Chandni Joshi

unread,
Jul 7, 2020, 2:23:36 PM7/7/20
to OHC-OPAL

I tried lookuplist first like this -- 
{{ '{' + '% radio' }} field="{{ f["model"] }}.{{ f["name"] }}" lookuplist="{{ f['enum'] }}" {{'%' +'}' }}

which resulted in 
{% radio field="TODOItem.job" lookuplist="['test1', 'test2']" %}

which looked like this in form (form.png) and while inspecting the code I found that its iterating over elements in a loop but then why it shows [[ item ]] that I didnt understand. 

do you think I am missing something here. I also saw few examples in elcid/templates/ <>.html forms like this 
{% radio model="formItem.editing.aerobic_or_anaerobic" label="Blood culture bottle type" lookuplist="['Aerobic', 'Anaerobic']" %}
dont know why its not working for me. 

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

Chandni Joshi

unread,
Jul 7, 2020, 3:09:19 PM7/7/20
to OHC-OPAL
Also, here is the Model ::

class TODOItem(models.EpisodeSubrecord):
job = fields.CharField(max_length=200,choices=enum('test1','test2'), blank=True, null=True)
due_date = fields.DateField(blank=True, null=True)
details = fields.TextField(blank=True, null=True)
completed = fields.BooleanField(default=False)

Fred Kingham

unread,
Jul 8, 2020, 5:32:11 AM7/8/20
to Chandni Joshi, OHC-OPAL
Gotcha!

So the issues is that OPAL uses a fusion of server side and angular rendering of a page {{ }} is when its rendered by django on the server, [[ ]] is when its rendered by angular (ie javascript) on the front end.

So to truly understand the output of the templating engine you need to view it as part of the opal application rather than just opening the html file.

(model, looks good btw)

Chandni Joshi

unread,
Jul 11, 2020, 11:56:28 AM7/11/20
to OHC-OPAL
Thank you Fred, 

I suspected that its related to angularJs as I tried few other forms with radio buttons and they had the same kind of output for iterable fields, which supported that its something else not the code. But need your guidance.

I will go through the documents and will update with the final form view. 

Also simultaneously, I can help with the documentation if needed, as I am trying to understand OPAL. Please let me know if I can be of any help here. 

Chandni Joshi

unread,
Jul 13, 2020, 5:36:13 PM7/13/20
to OHC-OPAL
Hi, 

This is what the form looks like. I followed the OPAL documentation further and was able to understand how to proceed further. 

Please find the attached form view.
Untitled.png

Fred Kingham

unread,
Jul 14, 2020, 4:20:40 AM7/14/20
to Chandni Joshi, OHC-OPAL
Looks great!

Chandni Joshi

unread,
Jul 16, 2020, 5:55:55 PM7/16/20
to OHC-OPAL
Hi, 

I am trying to create another application with the changed jinja file but it seems like whatever changes I do in record_form.jinja2 file, nothing is being reflected in the new html forms. 

I tried to clear cache using the following command without any success - python manage.py clear_cache() 


Looks great!

Fred Kingham

unread,
Jul 17, 2020, 5:25:21 AM7/17/20
to Chandni Joshi, OHC-OPAL
ok, so lets consider some potential problems.

Template inheritance
Template loading in django is done in an ordered fashion. If I have `tempates/onion.html` in opal and `templates/onion.html` in my_new_app. Django will look at my_naw_app and failing that will fail back to whatever is in opal.

Similarly when you run the ./manage.py scaffold Opal will look for the existence of a form in your app, then in opal and if it doesn't find it in either will not create it.

ie are we definitely creating a new file or is it just falling back to a file in opal? (ie does git status show a new file has been been created?).

That would be my first guess

Not sourcing the local version of opal
Are we definitely using the opal that you have editid locally. if you `pip install ipdb` and put it in `ipdb; ipdb.set_trace()` will it go into debug mode?

DEBUG and check you're loading in the correct template
Final suggestion is just put in an ipdb at the correct spot and figure out if we're getting the jinja template from where we thought it would be.

That would be how I would approach it. Hope that helps, let me know how it goes.

 


Chandni Joshi

unread,
Jul 23, 2020, 1:56:25 AM7/23/20
to OHC-OPAL
Thanks Fred for these pointers. I eventually found out that I was outside the virtual environment so none of my changes were going in the right place.

Do you think I can push my work on Opal repo on GitHub now for you to review. I saw that there are test cases for radio button with lookup list already in the opal tests. Is there any other scope for this form to test. 

Thanks,
Chandni
 

David Miller

unread,
Jul 23, 2020, 3:46:21 AM7/23/20
to Chandni Joshi, OHC-OPAL
Being outside the virtualenv is easy to do - I would normally check which e.g. Opal I'm running from with the command 

python -c 'import opal;print(opal.__file__)'

The explicit tests for this would be for the scaffold generation rather than the widgets themselves.
Probably in this test class: https://github.com/openhealthcare/opal/blob/v0.18.4/opal/tests/test_scaffold.py#L631

By all means open a PR if you like and we can discuss what tests need to be added there.

Best

Chandni Joshi

unread,
Aug 3, 2020, 9:14:51 PM8/3/20
to OHC-OPAL
Hi David, 

I tried to create a new branch with the command - git push origin feature/radio_button_scaffolding

but got the following error. 

remote: Permission to openhealthcare/opal.git denied to C09.

fatal: unable to access 'https://github.com/openhealthcare/opal.git/': The requested URL returned error: 403

David Miller

unread,
Aug 4, 2020, 5:40:12 AM8/4/20
to Chandni Joshi, OHC-OPAL
Hi, 

The way Github works you'd have to 

fork the repo
push your branch to your own copy
open a pull request from the forked repo to the main opal one

HTH

Chandni Joshi

unread,
Aug 4, 2020, 1:12:38 PM8/4/20
to OHC-OPAL
Oh ok, will do so. Sorry for such a basic mistake and bothering you guys with all these queries.. Thank you so much for responding to all of them with patience. 

Reply all
Reply to author
Forward
0 new messages