Skip to content

Commit

Permalink
Add fix and tests for generic tagging on proxy models
Browse files Browse the repository at this point in the history
  • Loading branch information
mailbackwards authored and jdufresne committed May 25, 2019
1 parent 20ab5ff commit f06b40c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions taggit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def lookup_kwargs(cls, instance):
@classmethod
def tags_for(cls, model, instance=None, **extra_filters):
tag_relname = cls.tag_relname()
model = model._meta.concrete_model
kwargs = {
"%s__content_type__app_label" % tag_relname: model._meta.app_label,
"%s__content_type__model" % tag_relname: model._meta.model_name,
Expand Down
6 changes: 6 additions & 0 deletions tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ class Migration(migrations.Migration):
],
options={"abstract": False},
),
migrations.CreateModel(
name="ProxyPhoto",
fields=[],
options={"proxy": True, "indexes": []},
bases=("tests.photo",),
),
migrations.CreateModel(
name="TaggedCustomPK",
fields=[
Expand Down
5 changes: 5 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ class Movie(Media):
pass


class ProxyPhoto(Photo):
class Meta:
proxy = True


class ArticleTag(Tag):
class Meta:
proxy = True
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
OfficialThroughModel,
Pet,
Photo,
ProxyPhoto,
TaggedCustomPK,
TaggedCustomPKFood,
TaggedFood,
Expand Down Expand Up @@ -628,6 +629,17 @@ def test_abstract_subclasses(self):
m.tags.add("hd")
self.assert_tags_equal(m.tags.all(), ["hd"])

def test_proxy_subclasses(self):
p = Photo.objects.create()
proxy_p = ProxyPhoto.objects.create()
p.tags.add("outdoors", "pretty")
self.assert_tags_equal(p.tags.all(), ["outdoors", "pretty"])
self.assert_tags_equal(proxy_p.tags.all(), [])

proxy_p.tags.add("hd")
self.assert_tags_equal(proxy_p.tags.all(), ["hd"])
self.assert_tags_equal(p.tags.all(), ["outdoors", "pretty"])

def test_field_api(self):
# Check if tag field, which simulates m2m, has django-like api.
field = self.food_model._meta.get_field("tags")
Expand Down

0 comments on commit f06b40c

Please sign in to comment.