Commit a38ee9d8 by Florian Apolloner

Merge pull request #213 from nicholasserra/nicholasserra-pluggable-manager

Allow pluggable manager for TaggableManager.
parents 1682de06 a00fbc0a
...@@ -69,6 +69,16 @@ playing around with the API. ...@@ -69,6 +69,16 @@ playing around with the API.
>>> apple.tags.slugs() >>> apple.tags.slugs()
[u'green-and-juicy', u'red'] [u'green-and-juicy', u'red']
.. hint::
You can subclass ``_TaggableManager`` (note the underscore) to add
methods or functionality. ``TaggableManager`` takes an optional
manager keyword argument for your custom class, like this::
class Food(models.Model):
# ... fields here
tags = TaggableManager(manager=_CustomTaggableManager)
Filtering Filtering
~~~~~~~~~ ~~~~~~~~~
......
...@@ -184,3 +184,11 @@ class Article(models.Model): ...@@ -184,3 +184,11 @@ class Article(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
tags = TaggableManager(through=ArticleTaggedItem) tags = TaggableManager(through=ArticleTaggedItem)
class CustomManager(models.Model):
class Foo(object):
def __init__(*args, **kwargs):
pass
tags = TaggableManager(manager=Foo)
...@@ -13,14 +13,14 @@ from django.utils.encoding import force_text ...@@ -13,14 +13,14 @@ from django.utils.encoding import force_text
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager, _model_name from taggit.managers import TaggableManager, _TaggableManager, _model_name
from taggit.models import Tag, TaggedItem from taggit.models import Tag, TaggedItem
from .forms import (FoodForm, DirectFoodForm, CustomPKFoodForm, from .forms import (FoodForm, DirectFoodForm, CustomPKFoodForm,
OfficialFoodForm) OfficialFoodForm)
from .models import (Food, Pet, HousePet, DirectFood, DirectPet, from .models import (Food, Pet, HousePet, DirectFood, DirectPet,
DirectHousePet, TaggedPet, CustomPKFood, CustomPKPet, CustomPKHousePet, DirectHousePet, TaggedPet, CustomPKFood, CustomPKPet, CustomPKHousePet,
TaggedCustomPKPet, OfficialFood, OfficialPet, OfficialHousePet, TaggedCustomPKPet, OfficialFood, OfficialPet, OfficialHousePet,
OfficialThroughModel, OfficialTag, Photo, Movie, Article) OfficialThroughModel, OfficialTag, Photo, Movie, Article, CustomManager)
from taggit.utils import parse_tags, edit_string_for_tags from taggit.utils import parse_tags, edit_string_for_tags
...@@ -355,7 +355,6 @@ class TaggableManagerTestCase(BaseTaggingTestCase): ...@@ -355,7 +355,6 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
'apple': set(['1', '2']) 'apple': set(['1', '2'])
}) })
class TaggableManagerDirectTestCase(TaggableManagerTestCase): class TaggableManagerDirectTestCase(TaggableManagerTestCase):
food_model = DirectFood food_model = DirectFood
pet_model = DirectPet pet_model = DirectPet
...@@ -391,6 +390,16 @@ class TaggableManagerOfficialTestCase(TaggableManagerTestCase): ...@@ -391,6 +390,16 @@ class TaggableManagerOfficialTestCase(TaggableManagerTestCase):
self.assertEqual(apple, self.food_model.objects.get(tags__official=False)) self.assertEqual(apple, self.food_model.objects.get(tags__official=False))
class TaggableManagerInitializationTestCase(TaggableManagerTestCase):
"""Make sure manager override defaults and sets correctly."""
food_model = Food
custom_manager_model = CustomManager
def test_default_manager(self):
self.assertEqual(self.food_model.tags.__class__, _TaggableManager)
def test_custom_manager(self):
self.assertEqual(self.custom_manager_model.tags.__class__, CustomManager.Foo)
class TaggableFormTestCase(BaseTaggingTestCase): class TaggableFormTestCase(BaseTaggingTestCase):
form_class = FoodForm form_class = FoodForm
......
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