On Oct 19, 10:51 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> > SELECT published.title, journal_names.name,
> > journal_impact_factor.impact_factor
> > FROM published, journal_names, journal_impact_factor
> > WHERE
> >      published.journal_names_id=journal_names.id
> >      AND
> >      published.year=journal_impact_factor.year
> >      AND
> >      journal_impact_factor.journal_names_id=journal_names.id;
>
> > Now, I can get publication title and journal name via:
> >      latest_pubs_list = published.objects.all().order_by('-date')[:10]
> >      [ (p.title, p.journal_names.name) for p in latest_pubs_list]
>
> > But I'm stumped on how to bring in the
> > journal_impact_factor.impact_factor.  Ideas?
>
> You're on the right track. The key bit you're missing is that you how to
> refer to reverse relations. That is described in [1].
>
> It's also necessary to realise that the many-to-one (ForeignKey) mapping
> between journal_impact_factor and journal means there can be multiple
> impact factors for a single journal (the reverse of many-to-one is one
> to *many*, after all). However, if you know for other reasons that
> you'll only have one impact for each journal, you can do this:
>
>         [(p.title, p.journal_names.name,
>            p.journal_names.journal_impact_factor_set.all()[0].impact_factor)
>            for p in latest_pubs_list]
>
> If you're going to be doing that query a lot, you might also want to
> look at the select_related() method on queyrsets (see [2]), so that you
> pull all the data back from the database in one query, rather than
> making three separate runs to the db (one for each access to a remote
> model).
>
> [1]http://www.djangoproject.com/documentation/db-api/#backward
> [2]http://www.djangoproject.com/documentation/db-api/#select-related

Malcolm,

Many thanks for the reply.  Seeing the example you provided in my
context (as opposed to the tutorial's context) made everything clear.
For some reason, entry_set actually relating to Entry didn't click for
me in the tutorial despite it saying so in the text!

To address what you've said, the additional constraint between the
join of published, journal_names and journal_impact_factor (which
isn't in there, but I'll be adding that unique_together as it's an
oversight) is that there is only one impact_factor for a particular
year .  In my case, I would limit that query to the associated
publication year.  What I've done, based on what you provided above
is:

[ { 'title' : p.title,
    'jname' : p.journal_names.name,
    'jif' :
p.journal_names.journal_impact_factor_set.filter(year=p.year)
[0].impact_factor
  } for p in latest_pubs_list ]

I switched to a dictionary for clarity and because I kept having a
weird problem trying to reference tuple elements in the template had
to generate the view.  Next is to explore the select_related and get a
little understanding in that bit of Django.

I very much appreciate the help and the insight.  Cheers!
-Tim


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to