Skip to content

Commit

Permalink
708 - Move Natural Key Manager back to models to avoid circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
Trafire committed Jul 15, 2024
1 parent 9639498 commit 1da39dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
14 changes: 0 additions & 14 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ def clone(self):
return type(self)(self.alias, self.col, self.content_types[:])


class NaturalKeyManager(models.Manager):
def __init__(self, natural_key_fields: List[str], *args, **kwargs):
super().__init__(*args, **kwargs)
self.natural_key_fields = natural_key_fields

def get_by_natural_key(self, *args):
if len(args) != len(self.model.natural_key_fields):
raise ValueError(
"Number of arguments does not match number of natural key fields."
)
lookup_kwargs = dict(zip(self.model.natural_key_fields, args))
return self.get(**lookup_kwargs)


class _TaggableManager(models.Manager):
# TODO investigate whether we can use a RelatedManager instead of all this stuff
# to take advantage of all the Django goodness
Expand Down
16 changes: 14 additions & 2 deletions taggit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from django.utils.translation import gettext_lazy as _
from django.utils.translation import pgettext_lazy

from taggit.managers import NaturalKeyManager

try:
from unidecode import unidecode
except ImportError:
Expand All @@ -19,6 +17,20 @@ def unidecode(tag):
return tag


class NaturalKeyManager(models.Manager):
def __init__(self, natural_key_fields: List[str], *args, **kwargs):
super().__init__(*args, **kwargs)
self.natural_key_fields = natural_key_fields

def get_by_natural_key(self, *args):
if len(args) != len(self.model.natural_key_fields):
raise ValueError(
"Number of arguments does not match number of natural key fields."
)
lookup_kwargs = dict(zip(self.model.natural_key_fields, args))
return self.get(**lookup_kwargs)


class NaturalKeyModel(models.Model):
def natural_key(self):
return (getattr(self, field) for field in self.natural_key_fields)
Expand Down

0 comments on commit 1da39dc

Please sign in to comment.