On Fri, 2007-10-19 at 15:04 -0700, Dr Stitch wrote:
> Greeting all!
> 
> CAVEAT:  I'm a Django newbie. I've spent about 3 hours searching,
> reading, hazing over all the documentation and tutorials and it's
> quite likely I have missed it or I'm way too new to Django to
> understand the simplicity of the solution for the problem I'm
> presenting.  So, feel free to "RTFURL" (provided you provide the
> URL :)  Okay, enough said.
> 
> The models.py from my app ("publications") is included at the end.  My
> end goal is to get the following:  'published.title,
> journal_names.name, journal_impact_factor.impact_factor'.
> 
> In standard SQL land, it would be something like:
> 
> 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

Regards,
Malcolm

-- 
Honk if you love peace and quiet. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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