|
5 | 5 | import warnings
|
6 | 6 | from decimal import Decimal
|
7 | 7 |
|
| 8 | +import django |
8 | 9 | import pytest
|
9 | 10 | from django.conf.urls import url
|
10 | 11 | from django.core.exceptions import ImproperlyConfigured
|
@@ -645,6 +646,51 @@ def test_must_call_distinct(self):
|
645 | 646 | )
|
646 | 647 |
|
647 | 648 |
|
| 649 | +class Blog(models.Model): |
| 650 | + name = models.CharField(max_length=20) |
| 651 | + |
| 652 | + |
| 653 | +class Entry(models.Model): |
| 654 | + blog = models.ForeignKey(Blog, on_delete=models.CASCADE) |
| 655 | + headline = models.CharField(max_length=120) |
| 656 | + pub_date = models.DateField(null=True) |
| 657 | + |
| 658 | + |
| 659 | +class BlogSerializer(serializers.ModelSerializer): |
| 660 | + class Meta: |
| 661 | + model = Blog |
| 662 | + fields = '__all__' |
| 663 | + |
| 664 | + |
| 665 | +class SearchFilterToManyTests(TestCase): |
| 666 | + |
| 667 | + @classmethod |
| 668 | + def setUpTestData(cls): |
| 669 | + b1 = Blog.objects.create(name='Blog 1') |
| 670 | + b2 = Blog.objects.create(name='Blog 2') |
| 671 | + |
| 672 | + # Multiple entries on Lennon published in 1979 - distinct should deduplicate |
| 673 | + Entry.objects.create(blog=b1, headline='Something about Lennon', pub_date=datetime.date(1979, 1, 1)) |
| 674 | + Entry.objects.create(blog=b1, headline='Another thing about Lennon', pub_date=datetime.date(1979, 6, 1)) |
| 675 | + |
| 676 | + # Entry on Lennon *and* a separate entry in 1979 - should not match |
| 677 | + Entry.objects.create(blog=b2, headline='Something unrelated', pub_date=datetime.date(1979, 1, 1)) |
| 678 | + Entry.objects.create(blog=b2, headline='Retrospective on Lennon', pub_date=datetime.date(1990, 6, 1)) |
| 679 | + |
| 680 | + @unittest.skipIf(django.VERSION < (1, 9), "Django 1.8 does not support transforms") |
| 681 | + def test_multiple_filter_conditions(self): |
| 682 | + class SearchListView(generics.ListAPIView): |
| 683 | + queryset = Blog.objects.all() |
| 684 | + serializer_class = BlogSerializer |
| 685 | + filter_backends = (filters.SearchFilter,) |
| 686 | + search_fields = ('=name', 'entry__headline', '=entry__pub_date__year') |
| 687 | + |
| 688 | + view = SearchListView.as_view() |
| 689 | + request = factory.get('/', {'search': 'Lennon,1979'}) |
| 690 | + response = view(request) |
| 691 | + assert len(response.data) == 1 |
| 692 | + |
| 693 | + |
648 | 694 | class OrderingFilterModel(models.Model):
|
649 | 695 | title = models.CharField(max_length=20, verbose_name='verbose title')
|
650 | 696 | text = models.CharField(max_length=100)
|
|
0 commit comments