0👍
This is either no longer true or perhaps the way you were checking the type of the elements was inadvertently triggering the evaluation.
To summarize, you can use append()
or my_tuple = my_tuple + (foo,)
. If you try to just print these it will evaluate the QuerySet and output their contents but if you loop through these collections you can work with the actual QuerySet
.
>>> a = Author.objects.filter()
>>> b = Book.objects.filter()
>>> type(a), type(b)
(<class 'django.db.models.query.QuerySet'>, <class 'django.db.models.query.QuerySet'>)
>>> l = []
>>> l.append(a)
>>> l.append(b)
>>> type(l[0]), type(l[1])
(<class 'django.db.models.query.QuerySet'>, <class 'django.db.models.query.QuerySet'>)
>>> for q in l:
... print type(q)
...
<class 'django.db.models.query.QuerySet'>
<class 'django.db.models.query.QuerySet'>
>>> my_tuple = ()
>>> my_tuple = my_tuple + (a,)
>>> type(my_tuple[0])
<class 'django.db.models.query.QuerySet'>
>>> len(l)
2
>>> len(my_tuple)
1
>>> print l
[[<Author: Author object>, '...(remaining elements truncated)...'], [<Book: Book object>,]]
>>> len(my_tuple)
1
>>> print my_tuple
([<Author: Author object>, '...(remaining elements truncated)...'],)
>>> len(my_tuple)
1
>>>
Source:stackexchange.com