Skip to content

Commit

Permalink
Merge pull request #112 from uncovertruth/feature/support_related
Browse files Browse the repository at this point in the history
Support related complement lookups
  • Loading branch information
tsuyukimakoto authored Aug 9, 2018
2 parents 6136459 + 67b4bdd commit 95b348a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.2.1
-----

- Support related `complement` lookups

0.2.0
-----

Expand Down
2 changes: 1 addition & 1 deletion lookup_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

__author__ = """UNCOVER TRUTH Inc."""
__email__ = '[email protected]'
__version__ = '0.2.0'
__version__ = '0.2.1'

default_app_config = 'lookup_extensions.apps.LookupExtensionsConfig'
5 changes: 4 additions & 1 deletion lookup_extensions/backends/base/operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Load lookup types
from lookup_extensions import lookups # noqa F401
from lookup_extensions import ( # noqa F401
lookups,
related_lookups,
)


class ExtendedDatabaseOperationsMixin(object):
Expand Down
2 changes: 1 addition & 1 deletion lookup_extensions/lookups/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .exists import * # noqa F401,F403
from .complements import * # noqa F401,F403
from .exregex import * # noqa F401,F403
from .negate import * # noqa F401,F403
File renamed without changes.
1 change: 1 addition & 0 deletions lookup_extensions/related_lookups/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .complements import * # noqa F401,F403
11 changes: 11 additions & 0 deletions lookup_extensions/related_lookups/complements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db.models.fields.related import ForeignObject
from django.db.models.fields.related_lookups import RelatedLookupMixin

from lookup_extensions.lookups import Complement


class RelatedComplement(RelatedLookupMixin, Complement):
pass


ForeignObject.register_lookup(RelatedComplement)
19 changes: 19 additions & 0 deletions tests/test_lookup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
try:
from lookup.models import (
Article,
Author,
Game,
Player,
Season,
Expand Down Expand Up @@ -1187,3 +1188,21 @@ def test_negate_complement(self):
'<Article: Article 1>',
],
)

def test_related_complement(self):
tags = Tag.objects.filter(articles__author=OuterRef('id'), name='Tag 2')
self.assertEqual(
[a.name for a in Author.objects.filter(
article__headline__startswith='Article',
article__tag__complement=Exists(tags)).distinct()],
['Author 1', 'Author 2'],
)

def test_negate_related_complement(self):
tags = Tag.objects.filter(articles__author=OuterRef('id'), name='Tag 2')
self.assertEqual(
[a.name for a in Author.objects.filter(
article__headline__startswith='Article',
article__tag__complement=~Exists(tags)).distinct()],
[],
)

0 comments on commit 95b348a

Please sign in to comment.