[Answered ]-Using data from django queries in the same view

2👍

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)

0👍

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).

Leave a comment