Skip to content

Instantly share code, notes, and snippets.

@hancush
Last active June 7, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hancush/c5126f8a74f594bb6f19be2e5c4cfed5 to your computer and use it in GitHub Desktop.
Save hancush/c5126f8a74f594bb6f19be2e5c4cfed5 to your computer and use it in GitHub Desktop.
from django.db.models import Q
condition = Q()
employers = Employer.objects.all()
if params:
if params.get('name'):
employers = employers.annotate(search_vector=SearchVector('name'))
name = Q(search_vector=params.get('name'))
condition &= name
if params.get('employer'):
name = Q(slug=params.get('employer'))
condition &= name
if params.get('parent'):
parent = Q(parent__slug=params.get('parent'))
condition &= parent
return employers.filter(condition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment