Seeing if a variable value is within a dictionary key value within a list of dictionaries

23 views
Skip to first unread message

Jason

unread,
Sep 7, 2012, 6:06:03 PM9/7/12
to django...@googlegroups.com
Hi there,

I'm using django 1.2 and am attempting to get the following code to work.

private_folder_details = [{"folderId":"1111", "name": "The folder name"},{"folderId":"1221", "name": "The other folder name"}]
private_folders = [{"id":"1111"},{"id":"2222"}]

{% for folder in private_folders %}
    {% if folder.id in private_folder_details %}
        <input type="checkbox" name="syncCheckbox" id="{{folder.id}}" checked="yes" value="{{folder.id}}">
    {% else %}
        <input type="checkbox" name="syncCheckbox" id="{{folder.id}}"  value="{{folder.id}}">
    {% endif %}
{% endfor %}

Essentially if folder.id is within any of the private_folder_details folderId's then I want the checkbox to be checked. What I'm doing above doesn't seem to work though - any idea how it could be made to work?


Tim Chase

unread,
Sep 7, 2012, 7:49:53 PM9/7/12
to django...@googlegroups.com, Jason
On 09/07/12 13:06, Jason wrote:
> I'm using django 1.2 and am attempting to get the following code to work.
>
> private_folder_details = [{"folderId":"1111", "name": "The folder
> name"},{"folderId":"1221", "name": "The other folder name"}]
> private_folders = [{"id":"1111"},{"id":"2222"}]
>
> {% for folder in private_folders %}
> * {% if folder.id in private_folder_details %}*
> <input type="checkbox" name="syncCheckbox" id="{{folder.id}}"
> checked="yes" value="{{folder.id}}">
> {% else %}
> <input type="checkbox" name="syncCheckbox" id="{{folder.id}}"
> value="{{folder.id}}">
> {% endif %}
> {% endfor %}
>
> Essentially if folder.id is within any of the private_folder_details
> folderId's then I want the checkbox to be checked. What I'm doing above
> doesn't seem to work though - any idea how it could be made to work?

A couple ideas occur to me, depending on how malleable your
underlying data-structure is and how much overlap there is between
them. My first thought is to change the private_folder_details to a
folderID->details mapping, something like


private_folder_details = [
{"folderId":"1111", "name": "The folder name"},
{"folderId":"1221", "name": "The other folder name"},
]
private_folder_details_1 = dict(
(d["folderId"], d)
for d in private_folder_details
)
private_folder_details_2 = set(
d["folderId"]
for d in private_folder_details
)

If all you need is the determination of whether a folder is/isn't a
private folder, use the second (set) version; if you need additional
information, use the former (dict) version. You can then test
whether the ID is in the set/dict, rather than if it's in the
list-of-dicts.

If for some reason you have multiple instances of the same folder-ID
in your list-of-dicts, you have to decide *why*, but should still be
able to use the second "set" version to gather folder-IDs of the
private folders.

-tkc




Craig Amundsen

unread,
Sep 7, 2012, 8:25:44 PM9/7/12
to django...@googlegroups.com
{% if folder.id in private_folder_details.values() %} should do the trick



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6wVcQQxDg_sJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Jason Whatford

unread,
Sep 7, 2012, 9:06:45 PM9/7/12
to django...@googlegroups.com
Hi Craig, thanks for the response. I'm using django 1.2 for templating and webapp2 to handle the requests on appengine. I've tried your suggestion of 

{% if folder.id in private_folder_details.values() %}

as well as 

{% if folder.id in private_folder_details.values %}

But it prevents page loading i.e. breaks the page. According to https://docs.djangoproject.com/en/1.2/ref/models/querysets/ it should work, as you suggest. Any ideas?

J

P.s. Tim, my data structure isn't that malleable. It's essentially I query my appengine datastore, and that retrieves a list of the data models, each of which can be treated as a dict. The dicts are used in a few different ways in the view - only this is giving me trouble though.

Craig Amundsen

unread,
Sep 7, 2012, 9:34:27 PM9/7/12
to django...@googlegroups.com
I see it now. private_datails is not a dictionary. It's a list of dictionaries where each dict has one key-value pair. How about this:

private_ids = [x.values()[0] for x in private_details]
if folder.id in private_ids

or if you want it one line:  if folder.id in [x.values()[0] for x in private_details]

- Craig

Tim Chase

unread,
Sep 7, 2012, 11:19:57 PM9/7/12
to django...@googlegroups.com, Jason Whatford
On 09/07/12 16:06, Jason Whatford wrote:
> P.s. Tim, my data structure isn't that malleable. It's
> essentially I query my appengine datastore, and that retrieves a
> list of the data models, each of which can be treated as a dict.
> The dicts are used in a few different ways in the view - only
> this is giving me trouble though.

Ah, but the transformations I suggest can be done within your view
before passing it to your template.

-tkc


Javier Guerra Giraldez

unread,
Sep 8, 2012, 6:40:15 PM9/8/12
to django...@googlegroups.com, Jason Whatford
it's the only sensible way.

you wouldn't put the database access code in the template, right?
similarly, there's no need to deal with less-than-optimal structures
in the template. the view code is the appropriate place to gather all
relevant data, transform it to a presentation-friendly format and pass
to the template just for rendering.


--
Javier

Jason

unread,
Sep 8, 2012, 7:45:40 PM9/8/12
to django...@googlegroups.com, Jason Whatford
Hi guys, 

thank you for your responses. I think, following your advice, I'll have to abandon my attempts to handle this in the template, and sort this out in the views instead. 

Thanks again :)
J

Tim Chase

unread,
Sep 8, 2012, 8:13:58 PM9/8/12
to django...@googlegroups.com, Jason
On 09/08/12 14:45, Jason wrote:
> thank you for your responses. I think, following your advice, I'll have to
> abandon my attempts to handle this in the template, and sort this out in
> the views instead.

While it's *possible* to implement the logic in the template, it (1)
is inefficient and (2) belongs in the view anyways. :-)

So you're making the right choice.

-tkc


Reply all
Reply to author
Forward
0 new messages