Combining Two Querysets in Django (With Different Models)
Today, I stumbled upon a use case where I needed to have a querysets that had objects from different models. Django has a neat "contenttypes framework" which is a good way to achieve this. So here are my notes on what I learned today, and I hope it will help someone in the future. NOTE : If you can design your models from scratch this is not the best approach to follow. Read my note under step 5. 1 The Models Let us consider the following models: class Bmw ( models . Model ) : series = models . CharField ( max_length = 50 ) created = models . DateTimeField ( ) class Meta : ordering = [ '-created' ] def __str__ ( self ) : return "{0} - {1}" . format ( self . series , self . created . date ( ) ) class Tesla ( models . Model ) : series = models . CharField ( max_length = 50 ) created = models . DateTimeField ( ) class Meta : ordering = [ '-created' ] def __str__ ( self )