On Wed, 2009-05-06 at 13:14 -0700, jrs_66 wrote:
> Thanks!  This is a great starting point.  My real queryset requires me
> to go a few joins deeper, however.  I will try nesting loops to get
> there, but this seems frightfully like querying in a loop (maybe I'm
> wrong, I'll check the end query to find out).  It also seems like a
> frightening amount of code to write to to produce a query which
> ultimately looks like this in SQL.
> 
>     #    SELECT containers.* FROM containers\
>     #    INNER JOIN containers_and_categories ON\
>     #        containers.id = containers_and_categories.container_id\
>     #    INNER JOIN flattened_category ON\
>     #        containers_and_categories.category_id =
> flattened_category.member_of_category_id\
>     #    WHERE flattened_category.member_of_category_id = %s",
> [request.POST["cat-id"]])

Okay, so your question is to restrict the results you're getting back.
That isn't quite what you asked. You mentioned what didn't work and
people were trying to explain how to access related objects given an
instance object, which was a reasonable guess. However, it wasn't too
easy to work out what you were trying to do, so, for next time, a bit of
an example of "here's the information I want to get at" would probably
help.

You haven't specified what the Container model looks like, but based on
the query you've written, I'd gues you're after

        
Container.objects.filter(category__flattenedcategory__id=request.POST["cat-id"])
        
The documentation for traversing relations inside filters is here:
http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

Querysets are iteratively (each method call being an iteration)
restricted sets of results. Predominantly filter() and exclude() being
used to restrict the results. What you were trying to do originally,
accessing an attribute, cannot work, because it is an attribute access
on the Queryset -- which doesn't have that kind of attribute -- not on a
Category model instance.

Regards,
Malcolm


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

Reply via email to