Skip to content

Commit 95b348a

Browse files
Merge pull request #112 from uncovertruth/feature/support_related
Support related complement lookups
2 parents 6136459 + 67b4bdd commit 95b348a

File tree

8 files changed

+42
-3
lines changed

8 files changed

+42
-3
lines changed

HISTORY.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
History
33
=======
44

5+
0.2.1
6+
-----
7+
8+
- Support related `complement` lookups
9+
510
0.2.0
611
-----
712

lookup_extensions/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
__author__ = """UNCOVER TRUTH Inc."""
44
__email__ = '[email protected]'
5-
__version__ = '0.2.0'
5+
__version__ = '0.2.1'
66

77
default_app_config = 'lookup_extensions.apps.LookupExtensionsConfig'

lookup_extensions/backends/base/operations.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Load lookup types
2-
from lookup_extensions import lookups # noqa F401
2+
from lookup_extensions import ( # noqa F401
3+
lookups,
4+
related_lookups,
5+
)
36

47

58
class ExtendedDatabaseOperationsMixin(object):

lookup_extensions/lookups/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .exists import * # noqa F401,F403
1+
from .complements import * # noqa F401,F403
22
from .exregex import * # noqa F401,F403
33
from .negate import * # noqa F401,F403
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .complements import * # noqa F401,F403
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.db.models.fields.related import ForeignObject
2+
from django.db.models.fields.related_lookups import RelatedLookupMixin
3+
4+
from lookup_extensions.lookups import Complement
5+
6+
7+
class RelatedComplement(RelatedLookupMixin, Complement):
8+
pass
9+
10+
11+
ForeignObject.register_lookup(RelatedComplement)

tests/test_lookup/tests.py

+19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
try:
1717
from lookup.models import (
1818
Article,
19+
Author,
1920
Game,
2021
Player,
2122
Season,
@@ -1187,3 +1188,21 @@ def test_negate_complement(self):
11871188
'<Article: Article 1>',
11881189
],
11891190
)
1191+
1192+
def test_related_complement(self):
1193+
tags = Tag.objects.filter(articles__author=OuterRef('id'), name='Tag 2')
1194+
self.assertEqual(
1195+
[a.name for a in Author.objects.filter(
1196+
article__headline__startswith='Article',
1197+
article__tag__complement=Exists(tags)).distinct()],
1198+
['Author 1', 'Author 2'],
1199+
)
1200+
1201+
def test_negate_related_complement(self):
1202+
tags = Tag.objects.filter(articles__author=OuterRef('id'), name='Tag 2')
1203+
self.assertEqual(
1204+
[a.name for a in Author.objects.filter(
1205+
article__headline__startswith='Article',
1206+
article__tag__complement=~Exists(tags)).distinct()],
1207+
[],
1208+
)

0 commit comments

Comments
 (0)