filter two columns django

14 views
Skip to first unread message

omar ahmed

unread,
Mar 17, 2019, 9:55:08 AM3/17/19
to Django users
hi guys
this is my view
def premierleague(request):
news_list = LeagueNews.objects.filter((publication_date__lte=timezone.now)|(league_name='english')).order_by('-publication_date')[:10]
return render(request, 'core/premierleague.html', {'news_list':news_list})
i want to filter my objects by two columns
publication_date and league_name
but it doesn't work

Shashank Singh

unread,
Mar 17, 2019, 10:02:46 AM3/17/19
to django...@googlegroups.com
OR
from django.db.models import Q
news_list = LeagueNews.objects.filter(Q(publication_date__lte=timezone.now)|Q(league_name='english')).order_by('-publication_date')[:10]

AND
news_list = LeagueNews.objects.filter(publication_date__lte=timezone.now, league_name='english').order_by('-publication_date')[:10]

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/58a97b33-b41e-4f87-a003-e9f82bcb9790%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

omar ahmed

unread,
Mar 17, 2019, 11:37:29 AM3/17/19
to Django users
thank you Shashank very much
it works
Reply all
Reply to author
Forward
0 new messages