Commit 45952169 by Florian Apolloner

Allow to specify a related_name to enable multiple tagging relations on one model.

This is pretty barebone for now, but should work till a nice solution is found
(Model validation in Django is stricter than it needs to be :/).
parent 5a9f2314
...@@ -29,8 +29,8 @@ def _model_name(model): ...@@ -29,8 +29,8 @@ def _model_name(model):
class TaggableRel(ManyToManyRel): class TaggableRel(ManyToManyRel):
def __init__(self, field): def __init__(self, field, related_name):
self.related_name = None self.related_name = related_name
self.limit_choices_to = {} self.limit_choices_to = {}
self.symmetrical = True self.symmetrical = True
self.multiple = True self.multiple = True
...@@ -69,11 +69,13 @@ class ExtraJoinRestriction(object): ...@@ -69,11 +69,13 @@ class ExtraJoinRestriction(object):
class TaggableManager(RelatedField, Field): class TaggableManager(RelatedField, Field):
def __init__(self, verbose_name=_("Tags"), _related_name_counter = 0
help_text=_("A comma-separated list of tags."), through=None, blank=False):
def __init__(self, verbose_name=_("Tags"), help_text=_("A comma-separated list of tags."),
through=None, blank=False, related_name=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) self.rel = TaggableRel(self, related_name)
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:
...@@ -119,8 +121,10 @@ class TaggableManager(RelatedField, Field): ...@@ -119,8 +121,10 @@ class TaggableManager(RelatedField, Field):
self.rel.to = self.through._meta.get_field("tag").rel.to self.rel.to = self.through._meta.get_field("tag").rel.to
self.related = RelatedObject(self.through, cls, self) self.related = RelatedObject(self.through, cls, self)
if self.use_gfk: if self.use_gfk:
tagged_items = GenericRelation(self.through) self.__class__._related_name_counter += 1
tagged_items.contribute_to_class(cls, "tagged_items") related_name = '+%d' % self.__class__._related_name_counter
tagged_items = GenericRelation(self.through, related_name=related_name)
tagged_items.contribute_to_class(cls, 'tagged_items')
def save_form_data(self, instance, value): def save_form_data(self, instance, value):
getattr(instance, self.name).set(*value) getattr(instance, self.name).set(*value)
......
...@@ -8,6 +8,11 @@ from taggit.models import (TaggedItemBase, GenericTaggedItemBase, TaggedItem, ...@@ -8,6 +8,11 @@ from taggit.models import (TaggedItemBase, GenericTaggedItemBase, TaggedItem,
TagBase, Tag) TagBase, Tag)
class MultipleTags(models.Model):
tags1 = TaggableManager()
tags2 = TaggableManager(related_name='+')
@python_2_unicode_compatible @python_2_unicode_compatible
class Food(models.Model): class Food(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
......
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