Commit 6e9043a1 by Daniel Hahler

Handle "to" with deconstruction

This accepts "to" as kwarg, and provides it via `deconstruct`, and also
changes `deconstruct` to always provide/set "through".

This is more explicit and since the manager always uses a through model
internally, this describes it better.
parent 0cc67dc3
...@@ -73,11 +73,12 @@ class TaggableManager(RelatedField, Field): ...@@ -73,11 +73,12 @@ class TaggableManager(RelatedField, Field):
_related_name_counter = 0 _related_name_counter = 0
def __init__(self, verbose_name=_("Tags"), help_text=_("A comma-separated list of tags."), def __init__(self, verbose_name=_("Tags"), help_text=_("A comma-separated list of tags."),
through=None, blank=False, related_name=None): through=None, blank=False, related_name=None, to=None):
Field.__init__(self, verbose_name=verbose_name, help_text=help_text, blank=blank, null=True, serialize=False) Field.__init__(self, verbose_name=verbose_name, help_text=help_text, blank=blank, null=True, serialize=False)
self.through = through or TaggedItem self.through = through or TaggedItem
self.rel = TaggableRel(self, related_name, self.through) self.rel = TaggableRel(self, related_name, self.through)
self.swappable = False self.swappable = False
# NOTE: `to` is ignored, only used via `deconstruct`.
def __get__(self, instance, model): def __get__(self, instance, model):
if instance is not None and instance.pk is None: if instance is not None and instance.pk is None:
...@@ -99,9 +100,10 @@ class TaggableManager(RelatedField, Field): ...@@ -99,9 +100,10 @@ class TaggableManager(RelatedField, Field):
# Remove forced kwargs. # Remove forced kwargs.
for kwarg in ('serialize', 'null'): for kwarg in ('serialize', 'null'):
del kwargs[kwarg] del kwargs[kwarg]
# Add non-default arguments. # Add arguments related to relations.
if self.through is not TaggedItem: # Ref: https://github.com/alex/django-taggit/issues/206#issuecomment-37578676
kwargs['through'] = self.through kwargs['through'] = self.through
kwargs['to'] = self.through._meta.get_field("tag").rel.to
return name, path, args, kwargs return name, path, args, kwargs
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment