这要么不再正确,要么您检查元素类型的方式可能无意中触发了评估。
总而言之,您可以使用append()
或my_tuple = my_tuple + (foo,)
。如果您尝试仅打印这些内容,它将评估 QuerySet 并输出它们的内容,但如果您遍历这些集合,您可以使用实际的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
>>>