Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. filters = Q(is_default = False)
  2. # Build the excludes and filters dynamically
  3. if cut:
  4. filters = filters & Q(mailbagstats__num_letters2__gt = int(cut) )
  5.  
  6. >>> filter = Q(a=True)
  7. >>> filter = filter & Q(b=True)
  8. >>> filter.children
  9. [('a', True), ('b', True)]
  10. >>> filter.children.pop()
  11. ('b', True)
  12. >>> filter.children
  13. [('a', True)]
  14.  
  15. filters = []
  16. filters.append(Q(is_default = False))
  17. # Build the excludes and filters dynamically
  18. if cut:
  19. filters.append(Q(mailbagstats__num_letters2__gt = int(cut)))
  20.  
  21. # I want to pop the last one
  22. filters.pop()
  23.  
  24. # build the filter before making the query
  25. # Note that this call will remove an element from the filters list
  26. filters_for_query = reduce(lambda a, x: a & x, filters, filters.pop())
  27.  
  28. Model.objects.filter(filters_for_query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement