On 10/30/06, Ceph <[EMAIL PROTECTED]> wrote:
>
> What I need to pull is a list of Items using the data from the Render
> model that represents the latest Revision of an Item. I am currently
> doing this easily with raw SQL, but this seems like something that
> Django should be able to do for me.

It's a little unclear what the 'latest revision of an item' is, given
that there isn't any date or sequence detail in the revision; however:

Item.objects.filter(revision__render__title='foo').distinct()

will return a list of all items that have a revision that have a
render with a title of 'foo'. This query creates an SQL query with
item-revision and revision-render table joins, and adds a 'where'
clause on render.title='foo'. If you add additional query terms, you
can narrow down the list of returned items. The distinct() is required
to prevent an item getting returned twice when there are two
revisions/render objects that match the search criteria.

> I think I need to use reverse relationships using related_names, but
> I'm not positive.

related_names is not necessary, but might help make things more clear.
When you add a foreign key to a model, the related model has an
attribute added to it called 'XXX_set' (where XXX is the name of the
model with the ForeignKey definition); in your case, for example, Item
will have a 'revision_set' member, which will contain the set of all
revisions that are related to an item. In queries, you drop the _set
part of the name.

If you have a related_name parameter in the ForeignKey definition,
this name is used for the  related object set and the reverse queries.
So, if your Revision model contained

  item = models.ForeignKey(Item, related_name='item_revisions')

then each Item would have a member called 'item_revisions', and the
query I gave earlier would be:

Item.objects.filter(item_revisions__render__title='foo')

The query will end up functionally the same, but the plural in the
name might help you understand the results you get a little better.

> Are there any documents out there that outline more than simple
> relationship cases?

The model examples are probably your best bet here; these examples are
used as part of the test suite, so in some cases they are deliberately
complex and convoluted to test some obscure facet of the query system
(and act as an example of how that obscure facet works/should work).

Hope this helps,
Yours,
Russ Magee %-)

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

Reply via email to