本文整理汇总了Python中django_filters.filters.MultipleChoiceFilter.filter方法的典型用法代码示例。如果您正苦于以下问题:Python MultipleChoiceFilter.filter方法的具体用法?Python MultipleChoiceFilter.filter怎么用?Python MultipleChoiceFilter.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django_filters.filters.MultipleChoiceFilter
的用法示例。
在下文中一共展示了MultipleChoiceFilter.filter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_filtering_skipped_when_len_of_value_is_len_of_field_choices
# 需要导入模块: from django_filters.filters import MultipleChoiceFilter [as 别名]
# 或者: from django_filters.filters.MultipleChoiceFilter import filter [as 别名]
def test_filtering_skipped_when_len_of_value_is_len_of_field_choices(self):
qs = mock.Mock(spec=[])
f = MultipleChoiceFilter(name='somefield')
result = f.filter(qs, [])
self.assertEqual(len(f.field.choices), 0)
self.assertEqual(qs, result)
f.field.choices = ['some', 'values', 'here']
result = f.filter(qs, ['some', 'values', 'here'])
self.assertEqual(qs, result)
result = f.filter(qs, ['other', 'values', 'there'])
self.assertEqual(qs, result)
示例2: test_filtering
# 需要导入模块: from django_filters.filters import MultipleChoiceFilter [as 别名]
# 或者: from django_filters.filters.MultipleChoiceFilter import filter [as 别名]
def test_filtering(self):
qs = mock.Mock(spec=['filter'])
f = MultipleChoiceFilter(name='somefield')
with mock.patch('django_filters.filters.Q') as mockQclass:
mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock()
mockQclass.side_effect = [mockQ1, mockQ2]
f.filter(qs, ['value'])
self.assertEqual(mockQclass.call_args_list,
[mock.call(), mock.call(somefield='value')])
mockQ1.__ior__.assert_called_once_with(mockQ2)
qs.filter.assert_called_once_with(mockQ1.__ior__.return_value)
qs.filter.return_value.distinct.assert_called_once_with()
示例3: test_filtering_skipped_with_empty_list_value_and_some_choices
# 需要导入模块: from django_filters.filters import MultipleChoiceFilter [as 别名]
# 或者: from django_filters.filters.MultipleChoiceFilter import filter [as 别名]
def test_filtering_skipped_with_empty_list_value_and_some_choices(self):
qs = mock.Mock(spec=[])
f = MultipleChoiceFilter(name='somefield')
f.field.choices = ['some', 'values', 'here']
result = f.filter(qs, [])
self.assertEqual(qs, result)
示例4: test_filtering_requires_name
# 需要导入模块: from django_filters.filters import MultipleChoiceFilter [as 别名]
# 或者: from django_filters.filters.MultipleChoiceFilter import filter [as 别名]
def test_filtering_requires_name(self):
qs = mock.Mock(spec=['filter'])
f = MultipleChoiceFilter()
with self.assertRaises(TypeError):
f.filter(qs, ['value'])
示例5: test_filter_conjoined_true
# 需要导入模块: from django_filters.filters import MultipleChoiceFilter [as 别名]
# 或者: from django_filters.filters.MultipleChoiceFilter import filter [as 别名]
def test_filter_conjoined_true(self):
"""Tests that a filter with `conjoined=True` returns objects that
have all the values included in `value`. For example filter
users that have all of this books.
"""
book_kwargs = {'price': 1, 'average_rating': 1}
books = []
books.append(Book.objects.create(**book_kwargs))
books.append(Book.objects.create(**book_kwargs))
books.append(Book.objects.create(**book_kwargs))
books.append(Book.objects.create(**book_kwargs))
books.append(Book.objects.create(**book_kwargs))
books.append(Book.objects.create(**book_kwargs))
user1 = User.objects.create()
user2 = User.objects.create()
user3 = User.objects.create()
user4 = User.objects.create()
user5 = User.objects.create()
user1.favorite_books.add(books[0], books[1])
user2.favorite_books.add(books[0], books[1], books[2])
user3.favorite_books.add(books[1], books[2])
user4.favorite_books.add(books[2], books[3])
user5.favorite_books.add(books[4], books[5])
filter_list = (
((books[0].pk, books[0].pk), # values
[1, 2]), # list of user.pk that have `value` books
((books[1].pk, books[1].pk),
[1, 2, 3]),
((books[2].pk, books[2].pk),
[2, 3, 4]),
((books[3].pk, books[3].pk),
[4, ]),
((books[4].pk, books[4].pk),
[5, ]),
((books[0].pk, books[1].pk),
[1, 2]),
((books[0].pk, books[2].pk),
[2, ]),
((books[1].pk, books[2].pk),
[2, 3]),
((books[2].pk, books[3].pk),
[4, ]),
((books[4].pk, books[5].pk),
[5, ]),
((books[3].pk, books[4].pk),
[]),
)
users = User.objects.all()
for item in filter_list:
f = MultipleChoiceFilter(field_name='favorite_books__pk', conjoined=True)
queryset = f.filter(users, item[0])
expected_pks = [c[0] for c in queryset.values_list('pk')]
self.assertListEqual(
expected_pks,
item[1],
'Lists Differ: {0} != {1} for case {2}'.format(
expected_pks, item[1], item[0]))