On 24 March 2012 09:29, gowtham <ragowtha...@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-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.