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