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 On Sat, Mar 24, 2012 at 3:36 AM, Sam Lai <samuel....@gmail.com> wrote: > 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. > > -- Gowthaman Bioinformatics Systems Programmer. SBRI, 307 West lake Ave N Suite 500 Seattle, WA. 98109-5219 Phone : LAB 206-256-7188 (direct). -- 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.