Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django append query results to query set

in a view I am constructing I need to consult multiple databases. What I want to do is use the results of on query_set to search another db table.

I have functioning mydb1_query_set, what I need now is something like this:

for row in mydb1_query_set:
        mydb2_query_set = Mytable.objects.filter(id=row.id)

So that I keep adding to the initially empty mydb2_query_set as I iterate. I realize that there is no QuerySet.append, so how do I achieve what I want? Any help much appreciated...

like image 204
Darwin Tech Avatar asked Nov 17 '11 17:11

Darwin Tech


1 Answers

Use a list instead of a queryset, and then you can append or extend as you wish.

mydb2_query = []
for row in mydb1_query_set:
    mydb2_query.extend(list(Mytable.objects.filter(id=row.id)))
like image 139
Alasdair Avatar answered Sep 24 '22 00:09

Alasdair