python - Django orm filter is an element of dict -
i'm trying set dict of filters orm queryset. here dict:
section = self.kwargs['section'] # hot or new or trending threshold = {'hot': 'post_upvotes > 50', 'trending': 'post_upvotes__range=(20,50)', 'new': 'post_upvotes < 20'} return post.objects.filter(threshold[section])
but doesn't work.
how can achieve ?
try this:
from django.db.models import q threshold = { 'hot': q(post_upvotes__gt=50), 'trending': q(post_upvotes__range=(20,50)), 'new': q(post_upvotes__lt=20) } return post.objects.filter(threshold[section])
read more q objects: https://docs.djangoproject.com/en/1.8/topics/db/queries/#complex-lookups-with-q-objects
Comments
Post a Comment