Commit acbbba89 by Florian Apolloner
parent a429c19c
......@@ -96,6 +96,7 @@ class TaggableManager(RelatedField, Field):
else:
self.set_attributes_from_name(name)
self.model = cls
cls._meta.add_field(self)
setattr(cls, name, self)
if not cls._meta.abstract:
......@@ -109,6 +110,7 @@ class TaggableManager(RelatedField, Field):
else:
self.post_through_setup(cls)
def __lt__(self, other):
"""
Required contribute_to_class as Django uses bisect
......@@ -125,11 +127,14 @@ class TaggableManager(RelatedField, Field):
self.rel.to = self.through._meta.get_field("tag").rel.to
self.related = RelatedObject(self.through, cls, self)
if self.use_gfk:
self.__class__._related_name_counter += 1
related_name = '+%d' % self.__class__._related_name_counter
tagged_items = GenericRelation(self.through, related_name=related_name)
tagged_items = GenericRelation(self.through)
tagged_items.contribute_to_class(cls, 'tagged_items')
for rel in cls._meta.local_many_to_many:
if isinstance(rel, TaggableManager) and rel.use_gfk and rel != self:
raise ValueError('You can only have one TaggableManager per model'
' using generic relations.')
def save_form_data(self, instance, value):
getattr(instance, self.name).set(*value)
......
......@@ -8,9 +8,18 @@ from taggit.models import (TaggedItemBase, GenericTaggedItemBase, TaggedItem,
TagBase, Tag)
# Ensure that two TaggableManagers with custom through model are allowed.
class Through1(TaggedItemBase):
content_object = models.ForeignKey('MultipleTags')
class Through2(TaggedItemBase):
content_object = models.ForeignKey('MultipleTags')
class MultipleTags(models.Model):
tags1 = TaggableManager()
tags2 = TaggableManager(related_name='+')
tags1 = TaggableManager(through=Through1, related_name='tags1')
tags2 = TaggableManager(through=Through2, related_name='tags2')
@python_2_unicode_compatible
......@@ -50,7 +59,7 @@ class TaggedPet(TaggedItemBase):
class DirectFood(models.Model):
name = models.CharField(max_length=50)
tags = TaggableManager(through="TaggedFood")
tags = TaggableManager(through='TaggedFood')
def __str__(self):
return self.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