filters.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import unicode_literals
  2. import django_filters
  3. import itertools
  4. from django import forms
  5. from django.db.models import Q
  6. from django.utils.encoding import force_text
  7. #
  8. # Filters
  9. #
  10. class NumericInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
  11. """
  12. Filters for a set of numeric values. Example: id__in=100,200,300
  13. """
  14. pass
  15. class NullableCharFieldFilter(django_filters.CharFilter):
  16. null_value = 'NULL'
  17. def filter(self, qs, value):
  18. if value != self.null_value:
  19. return super(NullableCharFieldFilter, self).filter(qs, value)
  20. qs = self.get_method(qs)(**{'{}__isnull'.format(self.name): True})
  21. return qs.distinct() if self.distinct else qs
  22. class NullableModelMultipleChoiceField(forms.ModelMultipleChoiceField):
  23. """
  24. This field operates like a normal ModelMultipleChoiceField except that it allows for one additional choice which is
  25. used to represent a value of Null. This is accomplished by creating a new iterator which first yields the null
  26. choice before entering the queryset iterator, and by ignoring the null choice during cleaning. The effect is similar
  27. to defining a MultipleChoiceField with:
  28. choices = [(0, 'None')] + [(x.id, x) for x in Foo.objects.all()]
  29. However, the above approach forces immediate evaluation of the queryset, which can cause issues when calculating
  30. database migrations.
  31. """
  32. iterator = forms.models.ModelChoiceIterator
  33. def __init__(self, null_value=0, null_label='None', *args, **kwargs):
  34. self.null_value = null_value
  35. self.null_label = null_label
  36. super(NullableModelMultipleChoiceField, self).__init__(*args, **kwargs)
  37. def _get_choices(self):
  38. if hasattr(self, '_choices'):
  39. return self._choices
  40. # Prepend the null choice to the queryset iterator
  41. return itertools.chain(
  42. [(self.null_value, self.null_label)],
  43. self.iterator(self),
  44. )
  45. choices = property(_get_choices, forms.ChoiceField._set_choices)
  46. def clean(self, value):
  47. # Strip all instances of the null value before cleaning
  48. if value is not None:
  49. stripped_value = [x for x in value if x != force_text(self.null_value)]
  50. else:
  51. stripped_value = value
  52. super(NullableModelMultipleChoiceField, self).clean(stripped_value)
  53. return value
  54. class NullableModelMultipleChoiceFilter(django_filters.ModelMultipleChoiceFilter):
  55. """
  56. This class extends ModelMultipleChoiceFilter to accept an additional value which implies "is null". The default
  57. queryset filter argument is:
  58. .filter(fieldname=value)
  59. When filtering by the value representing "is null" ('0' by default) the argument is modified to:
  60. .filter(fieldname__isnull=True)
  61. """
  62. field_class = NullableModelMultipleChoiceField
  63. def __init__(self, *args, **kwargs):
  64. self.null_value = kwargs.get('null_value', 0)
  65. super(NullableModelMultipleChoiceFilter, self).__init__(*args, **kwargs)
  66. def filter(self, qs, value):
  67. value = value or () # Make sure we have an iterable
  68. if self.is_noop(qs, value):
  69. return qs
  70. # Even though not a noop, no point filtering if empty
  71. if not value:
  72. return qs
  73. q = Q()
  74. for v in set(value):
  75. # Filtering by "is null"
  76. if v == force_text(self.null_value):
  77. arg = {'{}__isnull'.format(self.name): True}
  78. # Filtering by a related field (e.g. slug)
  79. elif self.field.to_field_name is not None:
  80. arg = {'{}__{}'.format(self.name, self.field.to_field_name): v}
  81. # Filtering by primary key (default)
  82. else:
  83. arg = {self.name: v}
  84. if self.conjoined:
  85. qs = self.get_method(qs)(**arg)
  86. else:
  87. q |= Q(**arg)
  88. if self.distinct:
  89. return self.get_method(qs)(q).distinct()
  90. return self.get_method(qs)(q)