Make filters of an object list
I get from my database a list of objects with attributes. So I want to make dynamics filters on the left depends of my list results.
For example, if i have an advertisement on "Paris" and "New York" i want to see Paris(1), New york (1) on the left. When you click on it, you will see just Paris' advertisement or New 开发者_开发知识库york's.
What is the best way to do that?
Thank you!
from django.db.models import Q
Advertisement.objects.filter( Q(name = 'Paris') | Q(name = 'New York') )
You need to use filters and Q objects.
You can use the aggregation https://docs.djangoproject.com/en/dev/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset
exemple:
cities = City.objects.annotate(ads_count=Count("adevertissement"))
精彩评论