[Fixed]-Django orm filter is an element of dict

1👍

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 about Q objects: https://docs.djangoproject.com/en/1.8/topics/db/queries/#complex-lookups-with-q-objects

Leave a comment