Accessing objects from a dictionary in templates

100 views
Skip to first unread message

gowtham

unread,
Mar 23, 2012, 3:56:01 PM3/23/12
to django...@googlegroups.com
Hi Everyone,
I am trying to pass a dictionary with numerical  as key and a object as value to my template...
i construct it like this:
reslibdic[res.result_id]=Library.objects.get(libraryresult__result__result_id=res.result_id)

But, in template, rather than iterating over the dictionary (using for and items)  i would like to access them using keys. So, i wrote the following template tag fiter.

def hash(h,key):
    if key in h:
        return h[key]
    else:
        return None

And in template i am using it like following. 
{{ reslibdic|hash:res.result_id }}

But, this gives me the primary key of the object NOT the OBJECT itself. I would like to get the object itself so that i can access and print its different values/fields.

Any idea? Or am I too stupid not being able to google this successfully since this morning.

I can alter the filter say return h[key].<field name> to work. But, i just dont think it is efficient to make one filter for each filed.


Thanks a bunch in advance,
Gowthaman


{code}
reslibdic = {}
allres = Result.objects.filter(resultslgene__geneid=geneid)
for res in allres:
reslibdic[res.result_id]=Library.objects.get(libraryresult__result__result_id=res.result_id)

{code}

Reinout van Rees

unread,
Mar 23, 2012, 4:41:21 PM3/23/12
to django...@googlegroups.com
On 23-03-12 20:56, gowtham wrote:
>
> But, in template, rather than iterating over the dictionary (using for
> and items) i would like to access them using keys.

You can, can't you? {{ my_dict.some_key }}.

You're using numerical keys, which might not work as well.
But you can try something like {{ my_dict.42 }}.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

gowtham

unread,
Mar 23, 2012, 5:00:58 PM3/23/12
to django...@googlegroups.com
Hi Reinout,
Thanks. 

But, i could not get it.  It prints the primary key of the stored object rather than the object itself.

Gowthaman

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




--
Gowthaman

Bioinformatics Systems Programmer.
SBRI, 307 West lake Ave N Suite 500
Seattle, WA. 98109-5219
Phone : LAB 206-256-7188 (direct).

Reinout van Rees

unread,
Mar 23, 2012, 6:07:15 PM3/23/12
to django...@googlegroups.com
On 23-03-12 22:00, gowtham wrote:
>
> But, i could not get it. It prints the primary key of the stored object
> rather than the object itself.

Well, I don't know what's in the stored object. How are you supposed to
print it?

Something like <b>{{ my_object.name }}</b>{{ my_object.description }} or so?

gowtham

unread,
Mar 23, 2012, 6:24:52 PM3/23/12
to django...@googlegroups.com
Yes, That is correct. 

Let me step back a bit and explain you what i am trying. It's quite possible i am doing the right thing.

I have two models (Library and Result) linked by a third linking model (libraryresult (has id, library_id and result_id fields FKeyed to respective tables). A many to many relationship.

[ And please point to me if I can query it in any other way?]

I want to pick a set of results (result_id) and want to get all the related libraries with them. 

1. So, i query Results first. 
allres = Result.objects.filter(resultslgene__geneid=geneid)

2. Then for each result_id, i get a library object and store it in a dictionary.
reslibdic[res.result_id]=Library.objects.get(libraryresult__result__result_id=res.result_id)

3. I pass both 'allres' objects list and 'reslibdic' dictionary to template to display

{% for res in allres %}
{{ res.result_id }} 
                {{ res.genome.organism.organismcode }} 
                {{ reslibdic.res.result_id.librarycode }} =? this does not work obviously. but indicates what i want to do....

{% endfor %}

In template, i want to access the all the library objects (passed as dictinary values) and print values for that.

Dictinoary works when I iterate via .items. But not direct access using keys. So, i wrote filters.....still i could either get only primary ids of the library object or with really customized filters (one for each field in library object) i get what i wanted....

Not sure if I clarified well or confused more

Anyway, i really appreciate your efforts to help me.
Gowthaman

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

gowtham

unread,
Mar 23, 2012, 6:25:44 PM3/23/12
to django...@googlegroups.com
sorry, I meant to say
"It's quite possible i am NOT doing the right thing."....

gowtham

unread,
Mar 23, 2012, 6:29:08 PM3/23/12
to django...@googlegroups.com
Template tag filters like this (made one for each field in the library object) helps me to get what i wanted.... But, that seems too silly to do...

in template
<td>{{ reslibdic|hash2libcode:res.result_id }}</td>


in template tag file:

def hash2libcode(h,key):
    if key in h:
        return h[key].librarycode
    else:
        return None

register.filter(hash2libcode)

Sam Lai

unread,
Mar 24, 2012, 6:36:02 AM3/24/12
to django...@googlegroups.com
On 24 March 2012 09:29, gowtham <ragow...@gmail.com> wrote:
> Template tag filters like this (made one for each field in the library
> object) helps me to get what i wanted.... But, that seems too silly to do...
>
> in template
> <td>{{ reslibdic|hash2libcode:res.result_id }}</td>
>

I had the same thought as Reinout did, but after your clarification I
realised you want to get something out of reslibdic by using another
variable as the key. AFAIK, there is no in-built way of doing this.

However, it is worth taking a step back and having a look at what
you're trying to do.

"I have two models (Library and Result) linked by a third linking
model (libraryresult (has id, library_id and result_id fields FKeyed
to respective tables). A many to many relationship."

By the sounds of things, you have actually defined a third model,
libraryresult, that links the Library and Result models together. That
third model is unnecessary (unless you have other attributes to store
with the link, which you don't seem to have - if you do, see
https://docs.djangoproject.com/en/1.4/topics/db/models/#intermediary-manytomany).

If you simply define a ManyToManyField on the Result model pointing to
the Library model, e.g.

class Result(models.Model):
# ...
libraries = models.ManyToManyField(Library)

... then you can get all the related libraries for a particular result
by simply doing -

r = Result.objects.get(pk=1)
print r.libraries.all()

You can do the same in the template. There is no need to build the
reslibdic object.

See https://docs.djangoproject.com/en/1.4/topics/db/models/#many-to-many-relationships
for help defining the model, and
https://docs.djangoproject.com/en/1.4/topics/db/queries/#many-to-many-relationships
for help querying the related objects.

> in template tag file:
>
> def hash2libcode(h,key):
>     if key in h:
>         return h[key].librarycode
>     else:
>         return None
>
> register.filter(hash2libcode)
>

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

gowtham

unread,
Mar 24, 2012, 10:59:53 AM3/24/12
to django...@googlegroups.com
Hi Samuel,
Thanks a ton for this detailed suggestion.  I was almost certain, the solution i was trying is not the optimal. Now its very clear. Thanks very very much. You made my weekend. I will be all busy backing up my database and implementing what you suggested.

Initially after reading your reply, i wondered, how can database handle it if there is no linking table. Especially, bulk of my data goes in using perl uploader. I started this project from designing the database & loading data first then adopting models from it. But, after reading one of your links, (Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship.) i am clear how it works. 

I might even try to use .through to adopt exisiting linking tables. But, will try NOT doing that as its a simple manytomany relationship as you observed.

Once again, THANKs very much for educating me,
Gowthaman


Reply all
Reply to author
Forward
0 new messages