Commit acbbba89 by Florian Apolloner
parent a429c19c
...@@ -96,6 +96,7 @@ class TaggableManager(RelatedField, Field): ...@@ -96,6 +96,7 @@ class TaggableManager(RelatedField, Field):
else: else:
self.set_attributes_from_name(name) self.set_attributes_from_name(name)
self.model = cls self.model = cls
cls._meta.add_field(self) cls._meta.add_field(self)
setattr(cls, name, self) setattr(cls, name, self)
if not cls._meta.abstract: if not cls._meta.abstract:
...@@ -109,6 +110,7 @@ class TaggableManager(RelatedField, Field): ...@@ -109,6 +110,7 @@ class TaggableManager(RelatedField, Field):
else: else:
self.post_through_setup(cls) self.post_through_setup(cls)
def __lt__(self, other): def __lt__(self, other):
""" """
Required contribute_to_class as Django uses bisect Required contribute_to_class as Django uses bisect
...@@ -125,11 +127,14 @@ class TaggableManager(RelatedField, Field): ...@@ -125,11 +127,14 @@ 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:
self.__class__._related_name_counter += 1 tagged_items = GenericRelation(self.through)
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') 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): def save_form_data(self, instance, value):
getattr(instance, self.name).set(*value) getattr(instance, self.name).set(*value)
......
...@@ -8,9 +8,18 @@ from taggit.models import (TaggedItemBase, GenericTaggedItemBase, TaggedItem, ...@@ -8,9 +8,18 @@ from taggit.models import (TaggedItemBase, GenericTaggedItemBase, TaggedItem,
TagBase, Tag) 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): class MultipleTags(models.Model):
tags1 = TaggableManager() tags1 = TaggableManager(through=Through1, related_name='tags1')
tags2 = TaggableManager(related_name='+') tags2 = TaggableManager(through=Through2, related_name='tags2')
@python_2_unicode_compatible @python_2_unicode_compatible
...@@ -50,7 +59,7 @@ class TaggedPet(TaggedItemBase): ...@@ -50,7 +59,7 @@ class TaggedPet(TaggedItemBase):
class DirectFood(models.Model): class DirectFood(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
tags = TaggableManager(through="TaggedFood") tags = TaggableManager(through='TaggedFood')
def __str__(self): def __str__(self):
return self.name 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