python - how to join three tables and show the items based on timestamps in django -


friends, working on site shows registered user his/her customized news feed based on users follow. want should see items 3 models question, answer , document, based on these items' timestamps.all models have timestamps field. how can implement it?

here views.py

def index(request):     questions = question.objects.all()     answers = answer.objects.all()     docs = document.objects.all()      return render(request,"welcome/index.html",locals()) 

i've kept views.py simple on here. want idea on how manipulate 3 models @ once.

you can chain objects, , sort using common timestamp field of models:

from itertools import chain  def index(request):     items = sorted(chain(question.objects.all(),                          answer.objects.all(),                          document.objects.all()), key=lambda obj: obj.timestamp)     return render(request, "welcome/index.html", {'items': items}) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -