Tips
Combine Querysets
From Combine 2 Django Querysets from Different Models:
If you’ve ever tried to concatenating two or more querysets from different
models (i.e. combined = queryset1 | queryset2
), you’ve hit this lovely
error:
Cannot combine queries on two different base models.
The solution to this is to use itertools:
from itertools import chain
result_list = list(chain(queryset1, queryset2))
This allows you to not only combine the querysets into a single iterable, but it also allows you to sort the entire set by a shared field such as the date created:
from itertools import chain
from operator import attrgetter
result_list = sorted(
chain(queryset1, queryset2),
key=attrgetter('date_created')
)
SQL
To display the SQL:
qs = Option.objects.filter(option_group__site=site)
print(qs.query)