tags:

views:

66

answers:

2

I might have missed somthing while searching through the documentation - I can't seem to find a way to use data from one query to form another query.

My query is:

sites_list = Site.objects.filter(worker=worker)

I'm trying to do something like this:

for site in sites_list:
    [Insert Query Here]

Edit: I saw the awnser and im not sure how i didnt get that, maybe thats the sign im up too late coding :S

+2  A: 

You could easily do something like this:

sites_list = Site.objects.filter(worker=worker)

for site in sites_list:
    new_sites_list = Site.objects.filter(name=site.name).filter(something else)
Dan Lorenc
A: 

You can also use the __in lookup type. For example, if you had an Entry model with a relation to Site, you could write:

Entry.objects.filter(site__in=Site.objects.filter(...some conditions...))

This will end up doing one query in the DB (the filter condition on sites would be turned into a subquery in the WHERE clause).

James Bennett