querySet1.union(querySet2, querySet3, etc) #Minimum 1 argument # Subscribe to GuruTheCoder.
# you need to have a foreign key relationship defined in the models.py first
class OrderModelName(models.Model):
order_id = models.IntegerField(primary_key=True, db_column='order_id')
item_id = models.ForeignKey(ItemModelName, db_column='item_id', on_delete=models.RESTRICT, null=True, blank=True)
# Then in views.py you can pull in foreign fields in your queryset using
# foreignkeyfield__fieldonforeigntable Example:
def your_view(request):
data = OrderModelName.objects.all().values(order_id, item_id__item_name)
stories = django_stories | vitor_stories # merge querysets
## GITHUB URL: https://stackoverflow.com/questions/431628/how-can-i-combine-two-or-more-querysets-in-a-django-view
from itertools import chain
result_list = list(chain(page_list, article_list, post_list))
matches = pages | articles | posts
#It retains all the functions of the querysets which is nice if you want to order_by or similar.
#Please note: this doesn't work on querysets from two different models.