On Wed, Feb 18, 2009 at 3:13 PM, May <adles...@gmail.com> wrote: > > Hello Karen, > > Yes, I seem confused here. I've been trying everything. The key is > that this query works correctly in the postgres database to retrieve > the institutionname (institution), using the contact id retrieved from > Project: > > select DISTINCT Institution.institution > from Contactintermed, Institution, Project > where Contactintermed.contact_id=Project.contact_id > and Contactintermed.institution_id=Institution.id > > What do you mean by "works correctly"? You mean it retrieves exactly one result? If so, that is an accident of the data you happen to have in your DB, not anything guaranteed by the models you have defined. As you have defined your models, any Contact might have multiple associated Institutions, so asking how to come up with THE institution associated with a Contact is futile. There isn't one, there is a list (accessible via the Contact.contactintermed_set related manager) of 0, 1, or more. If you want to print out all Institutions associated with a Contact, you can iterate through that set for each contact in the template, something like (warning -- untested syntax, I could be misremembering details):
{% for project in results %} {{project.contact|safe}} {% for ci in project.contact.contactintermed_set.all %} {{ ci.institution }} {% endfor %} {% endfor %} But there is nothing in your data models that guarantees the length of contactintermed_set for a given Contact is going to be 1. If there is something in the actual data model that guarantees that then your Django model definitions aren't properly reflecting your real data model. > In Python after I do a search by keyword in the view to get the > "results", I cannot get the template to display the institution name > (which I call institution). The query must first retrieve the Project > records. From the project records I can then grab the contactid, > which is then used in the Contactintermed table, which then goes to > the Institution table to retrieve the institution name (institution). > The "results" statement, doesn't seem to drill down through the > Contactintermed table, even though I've tried adding the > select_related to the statement. select_related doesn't affect what's accessible via a model, it's purely a performance optimization that affects how many SQL queries will be issued under the covers when following ForeignKey relationships. It's also not applicable here in terms of 'drilling down' to Contactintermed because neither your Project nor your Contact model has a ForeignKey to Contactintermed, rather Contactintermed is the one with the ForeignKeys back to Contact and Institution. Karen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---