Commit 473aac86 by Florian Apolloner

Merge pull request #251 from apollo13/dj1.7

Fixes for #238
parents f54b971e 97cafe1d
...@@ -252,8 +252,14 @@ class TaggableManager(RelatedField, Field): ...@@ -252,8 +252,14 @@ class TaggableManager(RelatedField, Field):
del kwargs[kwarg] del kwargs[kwarg]
# Add arguments related to relations. # Add arguments related to relations.
# Ref: https://github.com/alex/django-taggit/issues/206#issuecomment-37578676 # Ref: https://github.com/alex/django-taggit/issues/206#issuecomment-37578676
kwargs['through'] = self.through if isinstance(self.rel.through, six.string_types):
kwargs['to'] = self.through._meta.get_field("tag").rel.to kwargs['through'] = self.rel.through
elif not self.rel.through._meta.auto_created:
kwargs['through'] = "%s.%s" % (self.rel.through._meta.app_label, self.rel.through._meta.object_name)
if isinstance(self.rel.to, six.string_types):
kwargs['to'] = self.rel.to
else:
kwargs['to'] = '%s.%s' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name)
return name, path, args, kwargs return name, path, args, kwargs
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
...@@ -266,6 +272,10 @@ class TaggableManager(RelatedField, Field): ...@@ -266,6 +272,10 @@ class TaggableManager(RelatedField, Field):
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:
if isinstance(self.rel.to, six.string_types):
def resolve_related_class(field, model, cls):
field.rel.to = model
add_lazy_relation(cls, self, self.rel.to, resolve_related_class)
if isinstance(self.through, six.string_types): if isinstance(self.through, six.string_types):
def resolve_related_class(field, model, cls): def resolve_related_class(field, model, cls):
self.through = model self.through = model
...@@ -291,6 +301,7 @@ class TaggableManager(RelatedField, Field): ...@@ -291,6 +301,7 @@ class TaggableManager(RelatedField, Field):
self.use_gfk = ( self.use_gfk = (
self.through is None or issubclass(self.through, GenericTaggedItemBase) self.through is None or issubclass(self.through, GenericTaggedItemBase)
) )
if not self.rel.to:
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:
......
...@@ -6,7 +6,7 @@ from django.db import models, migrations ...@@ -6,7 +6,7 @@ from django.db import models, migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('contenttypes', '__first__'), ('contenttypes', '0001_initial'),
] ]
operations = [ operations = [
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import models, migrations from django.db import models, migrations
import taggit.models
import tests.models
import taggit.managers import taggit.managers
...@@ -29,7 +27,7 @@ class Migration(migrations.Migration): ...@@ -29,7 +27,7 @@ class Migration(migrations.Migration):
name='CustomManager', name='CustomManager',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('tags', taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags')), ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags')),
], ],
options={ options={
}, },
...@@ -98,7 +96,7 @@ class Migration(migrations.Migration): ...@@ -98,7 +96,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=50)), ('name', models.CharField(max_length=50)),
('tags', taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags')), ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags')),
], ],
options={ options={
}, },
...@@ -108,7 +106,7 @@ class Migration(migrations.Migration): ...@@ -108,7 +106,7 @@ class Migration(migrations.Migration):
name='Movie', name='Movie',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('tags', taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags')), ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags')),
], ],
options={ options={
'abstract': False, 'abstract': False,
...@@ -128,7 +126,7 @@ class Migration(migrations.Migration): ...@@ -128,7 +126,7 @@ class Migration(migrations.Migration):
name='MultipleTagsGFK', name='MultipleTagsGFK',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('tags1', taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags')), ('tags1', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags')),
], ],
options={ options={
}, },
...@@ -191,15 +189,15 @@ class Migration(migrations.Migration): ...@@ -191,15 +189,15 @@ class Migration(migrations.Migration):
bases=(models.Model,), bases=(models.Model,),
), ),
migrations.AddField( migrations.AddField(
model_name='officialpet', model_name='officialfood',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=tests.models.OfficialTag, through=tests.models.OfficialThroughModel, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='tests.OfficialTag', through='tests.OfficialThroughModel', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
model_name='officialfood', model_name='officialpet',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=tests.models.OfficialTag, through=tests.models.OfficialThroughModel, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='tests.OfficialTag', through='tests.OfficialThroughModel', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.CreateModel( migrations.CreateModel(
...@@ -225,14 +223,14 @@ class Migration(migrations.Migration): ...@@ -225,14 +223,14 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='pet', model_name='pet',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.CreateModel( migrations.CreateModel(
name='Photo', name='Photo',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('tags', taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags')), ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags')),
], ],
options={ options={
'abstract': False, 'abstract': False,
...@@ -252,7 +250,7 @@ class Migration(migrations.Migration): ...@@ -252,7 +250,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='custompkfood', model_name='custompkfood',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.TaggedCustomPKFood, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.TaggedCustomPKFood', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -280,7 +278,7 @@ class Migration(migrations.Migration): ...@@ -280,7 +278,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='custompkpet', model_name='custompkpet',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.TaggedCustomPKPet, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.TaggedCustomPKPet', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -308,7 +306,7 @@ class Migration(migrations.Migration): ...@@ -308,7 +306,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='directfood', model_name='directfood',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.TaggedFood, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.TaggedFood', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -336,7 +334,7 @@ class Migration(migrations.Migration): ...@@ -336,7 +334,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='directpet', model_name='directpet',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.TaggedPet, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.TaggedPet', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -364,7 +362,7 @@ class Migration(migrations.Migration): ...@@ -364,7 +362,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='multipletags', model_name='multipletags',
name='tags1', name='tags1',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.Through1, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.Through1', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -392,7 +390,7 @@ class Migration(migrations.Migration): ...@@ -392,7 +390,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='multipletags', model_name='multipletags',
name='tags2', name='tags2',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.Through2, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.Through2', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.AddField( migrations.AddField(
...@@ -423,7 +421,7 @@ class Migration(migrations.Migration): ...@@ -423,7 +421,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='multipletagsgfk', model_name='multipletagsgfk',
name='tags2', name='tags2',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.ThroughGFK, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.ThroughGFK', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
migrations.CreateModel( migrations.CreateModel(
...@@ -447,7 +445,7 @@ class Migration(migrations.Migration): ...@@ -447,7 +445,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='article', model_name='article',
name='tags', name='tags',
field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=tests.models.ArticleTaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags'), field=taggit.managers.TaggableManager(to='taggit.Tag', through='tests.ArticleTaggedItem', help_text='A comma-separated list of tags.', verbose_name='Tags'),
preserve_default=True, preserve_default=True,
), ),
] ]
...@@ -22,8 +22,8 @@ from taggit.models import Tag, TaggedItem ...@@ -22,8 +22,8 @@ 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, TaggedFood, CustomPKFood, CustomPKPet, CustomPKHousePet,
TaggedCustomPKPet, OfficialFood, OfficialPet, OfficialHousePet, TaggedCustomPKFood, OfficialFood, OfficialPet, OfficialHousePet,
OfficialThroughModel, OfficialTag, Photo, Movie, Article, CustomManager) 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
...@@ -298,13 +298,12 @@ class TaggableManagerTestCase(BaseTaggingTestCase): ...@@ -298,13 +298,12 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
) )
def test_taggeditem_unicode(self): def test_taggeditem_unicode(self):
ross = self.pet_model.objects.create(name="ross") apple = self.food_model.objects.create(name="apple")
# I keep Ross Perot for a pet, what's it to you? apple.tags.add("juicy")
ross.tags.add("president")
self.assertEqual( self.assertEqual(
force_text(self.taggeditem_model.objects.all()[0]), force_text(self.taggeditem_model.objects.all()[0]),
"ross tagged with president" "apple tagged with juicy"
) )
def test_abstract_subclasses(self): def test_abstract_subclasses(self):
...@@ -364,13 +363,13 @@ class TaggableManagerDirectTestCase(TaggableManagerTestCase): ...@@ -364,13 +363,13 @@ class TaggableManagerDirectTestCase(TaggableManagerTestCase):
food_model = DirectFood food_model = DirectFood
pet_model = DirectPet pet_model = DirectPet
housepet_model = DirectHousePet housepet_model = DirectHousePet
taggeditem_model = TaggedPet taggeditem_model = TaggedFood
class TaggableManagerCustomPKTestCase(TaggableManagerTestCase): class TaggableManagerCustomPKTestCase(TaggableManagerTestCase):
food_model = CustomPKFood food_model = CustomPKFood
pet_model = CustomPKPet pet_model = CustomPKPet
housepet_model = CustomPKHousePet housepet_model = CustomPKHousePet
taggeditem_model = TaggedCustomPKPet taggeditem_model = TaggedCustomPKFood
def test_require_pk(self): def test_require_pk(self):
# TODO with a charfield pk, pk is never None, so taggit has no way to # TODO with a charfield pk, pk is never None, so taggit has no way to
...@@ -557,10 +556,11 @@ class TagStringParseTestCase(UnitTestCase): ...@@ -557,10 +556,11 @@ class TagStringParseTestCase(UnitTestCase):
@skipIf(django.VERSION < (1, 7), "not relevant for Django < 1.7") @skipIf(django.VERSION < (1, 7), "not relevant for Django < 1.7")
class DeconstructTestCase(UnitTestCase): class DeconstructTestCase(UnitTestCase):
def test_deconstruct_kwargs_kept(self): def test_deconstruct_kwargs_kept(self):
instance = TaggableManager(through=OfficialThroughModel) instance = TaggableManager(through=OfficialThroughModel, to='dummy.To')
name, path, args, kwargs = instance.deconstruct() name, path, args, kwargs = instance.deconstruct()
new_instance = TaggableManager(*args, **kwargs) new_instance = TaggableManager(*args, **kwargs)
self.assertEqual(instance.rel.through, new_instance.rel.through) self.assertEqual('tests.OfficialThroughModel', new_instance.rel.through)
self.assertEqual('dummy.To', new_instance.rel.to)
@skipUnless(django.VERSION < (1, 7), "test only applies to 1.6 and below") @skipUnless(django.VERSION < (1, 7), "test only applies to 1.6 and below")
......
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