diff options
| author | Jannis Leidel <jannis@leidel.info> | 2010-01-09 20:03:52 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2010-01-09 20:03:52 +0000 |
| commit | bacfe3f3e8638433976bf1b12f16a8d975970884 (patch) | |
| tree | f2238e448679413eae366f9ba6e42605502c24ff /tests | |
| parent | 1e955e01430c7fdaf0ffe7e1da0d0ca71c1c4836 (diff) | |
Fixed #9638 - Added %(app_label)s to the related_name format string for abstract models.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12139 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
4 files changed, 76 insertions, 0 deletions
diff --git a/tests/modeltests/model_inheritance/models.py b/tests/modeltests/model_inheritance/models.py index 26ec0be503..36fc9fef1b 100644 --- a/tests/modeltests/model_inheritance/models.py +++ b/tests/modeltests/model_inheritance/models.py @@ -116,6 +116,31 @@ class ParkingLot(Place): def __unicode__(self): return u"%s the parking lot" % self.name +# +# Abstract base classes with related models where the sub-class has the +# same name in a different app and inherits from the same abstract base +# class. +# NOTE: The actual API tests for the following classes are in +# model_inheritance_same_model_name/models.py - They are defined +# here in order to have the name conflict between apps +# + +class Title(models.Model): + title = models.CharField(max_length=50) + +class NamedURL(models.Model): + title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set') + url = models.URLField() + + class Meta: + abstract = True + +class Copy(NamedURL): + content = models.TextField() + + def __unicode__(self): + return self.content + __test__ = {'API_TESTS':""" # The Student and Worker models both have 'name' and 'age' fields on them and # inherit the __unicode__() method, just as with normal Python subclassing. diff --git a/tests/modeltests/model_inheritance_same_model_name/__init__.py b/tests/modeltests/model_inheritance_same_model_name/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/model_inheritance_same_model_name/__init__.py diff --git a/tests/modeltests/model_inheritance_same_model_name/models.py b/tests/modeltests/model_inheritance_same_model_name/models.py new file mode 100644 index 0000000000..40de02764a --- /dev/null +++ b/tests/modeltests/model_inheritance_same_model_name/models.py @@ -0,0 +1,19 @@ +""" +XX. Model inheritance + +Model inheritance across apps can result in models with the same name resulting +in the need for an %(app_label)s format string. This app specifically tests +this feature by redefining the Copy model from model_inheritance/models.py +""" + +from django.db import models +from modeltests.model_inheritance.models import NamedURL + +# +# Abstract base classes with related models +# +class Copy(NamedURL): + content = models.TextField() + + def __unicode__(self): + return self.content diff --git a/tests/modeltests/model_inheritance_same_model_name/tests.py b/tests/modeltests/model_inheritance_same_model_name/tests.py new file mode 100644 index 0000000000..3f1e3458e6 --- /dev/null +++ b/tests/modeltests/model_inheritance_same_model_name/tests.py @@ -0,0 +1,32 @@ +from django.test import TestCase +from modeltests.model_inheritance.models import Title + +class InheritanceSameModelNameTests(TestCase): + + def setUp(self): + # The Title model has distinct accessors for both + # model_inheritance.Copy and model_inheritance_same_model_name.Copy + # models. + self.title = Title.objects.create(title='Lorem Ipsum') + + def test_inheritance_related_name(self): + from modeltests.model_inheritance.models import Copy + self.assertEquals( + self.title.attached_model_inheritance_copy_set.create( + content='Save $ on V1agr@', + url='http://v1agra.com/', + title='V1agra is spam', + ), Copy.objects.get(content='Save $ on V1agr@')) + + def test_inheritance_with_same_model_name(self): + from modeltests.model_inheritance_same_model_name.models import Copy + self.assertEquals( + self.title.attached_model_inheritance_same_model_name_copy_set.create( + content='The Web framework for perfectionists with deadlines.', + url='http://www.djangoproject.com/', + title='Django Rocks' + ), Copy.objects.get(content='The Web framework for perfectionists with deadlines.')) + + def test_related_name_attribute_exists(self): + # The Post model doesn't have an attribute called 'attached_%(app_label)s_%(class)s_set'. + self.assertEqual(hasattr(self.title, 'attached_%(app_label)s_%(class)s_set'), False) |
