Quantcast
Channel: Multi-model RSS Feed in Django - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by Razenstein for Multi-model RSS Feed in Django

$
0
0

you can use queryset3 = queryset1.union(queryset2) to concatenate items of two different models and use this in feed.items(self):

def items(self):    query1 = Article1.objects.filter(is_published=1)    query2 = Article2.objects.filter(is_published=1)    query = query1.union(query2)    # do any sorting AFTER union    query_sorted = query.order_by('a_field', 'another_field')       self.item_count = query_sorted.count()    return query_sorted

union() works as long as queries have the same fields.

If you have different field names but same content like "name" and "item_name" then you can rename the field in one of the queries:

    query2 = Article2.objects.filter(is_published=1).annotate(name=F('item_name')).values('name')

note: above is "pseudo" code - I did not run it so it might have syntax errors


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>