Ticket #9638: app_label.2.patch
File app_label.2.patch, 7.2 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/related.py
92 92 sup.contribute_to_class(cls, name) 93 93 94 94 if not cls._meta.abstract and self.rel.related_name: 95 self.rel.related_name = self.rel.related_name % {'class': cls.__name__.lower()} 95 self.rel.related_name = self.rel.related_name % { 96 'class': cls.__name__.lower(), 97 'app_label': cls._meta.app_label.lower(), 98 } 96 99 97 100 other = self.rel.to 98 101 if isinstance(other, basestring): -
tests/modeltests/model_inheritance/models.py
116 116 def __unicode__(self): 117 117 return u"%s the parking lot" % self.name 118 118 119 # 120 # Abstract base classes with related models where the sub-class has the 121 # same name in a different app and inherits from the same abstract base 122 # class. 123 # NOTE: The actual API tests for the following classes are in 124 # model_inheritance_same_model_name/models.py - They are defined 125 # here in order to have the name conflict between apps 126 # 127 128 class Title(models.Model): 129 title = models.CharField(max_length=50) 130 131 class NamedURL(models.Model): 132 title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set') 133 url = models.URLField() 134 135 class Meta: 136 abstract = True 137 138 class Copy(NamedURL): 139 content = models.TextField() 140 141 def __unicode__(self): 142 return self.content 143 119 144 __test__ = {'API_TESTS':""" 120 145 # The Student and Worker models both have 'name' and 'age' fields on them and 121 146 # inherit the __unicode__() method, just as with normal Python subclassing. -
tests/modeltests/model_inheritance_same_model_name/models.py
Property changes on: tests/modeltests/model_inheritance_same_model_name/__init__.py ___________________________________________________________________ Added: svn:eol-style + native
1 """ 2 XX. Model inheritance 3 4 Model inheritance across apps can result in models with the same name resulting 5 in the need for an %(app_label)s format string. This app specifically tests 6 this feature by redefining the Copy model from model_inheritance/models.py 7 """ 8 9 from django.db import models 10 from modeltests.model_inheritance.models import NamedURL, Title 11 12 # 13 # Abstract base classes with related models 14 # 15 class Copy(NamedURL): 16 content = models.TextField() 17 18 def __unicode__(self): 19 return self.content 20 21 22 __test__ = {'API_TESTS':""" 23 # Create a Title 24 >>> title = Title(title='Lorem Ipsum') 25 >>> title.save() 26 27 # The Title model has distinct accessors for both model_inheritance.Copy and 28 # model_inheritance_same_model_name.Copy models. 29 >>> title.attached_model_inheritance_copy_set.create(content='Save $ on V1agr@', url='http://v1agra.com/', title='V1agra is spam') 30 <Copy: Save $ on V1agr@> 31 >>> 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') 32 <Copy: The Web framework for perfectionists with deadlines.> 33 34 # The Post model doesn't have an attribute called 'attached_%(app_label)s_%(class)s_set'. 35 >>> getattr(title, 'attached_%(app_label)s_%(class)s_set') 36 Traceback (most recent call last): 37 ... 38 AttributeError: 'Title' object has no attribute 'attached_%(app_label)s_%(class)s_set' 39 40 """} -
AUTHORS
Property changes on: tests/modeltests/model_inheritance_same_model_name/models.py ___________________________________________________________________ Added: svn:eol-style + native
234 234 David Krauth 235 235 kurtiss@meetro.com 236 236 Panos Laganakos <panos.laganakos@gmail.com> 237 lakin.wecker@gmail.com237 Lakin Wecker <lakin.wecker@gmail.com> <lakin@structuredabstraction.com> 238 238 Nick Lane <nick.lane.au@gmail.com> 239 239 Stuart Langridge <http://www.kryogenix.org/> 240 240 Paul Lanier <planier@google.com> -
docs/topics/db/models.txt
858 858 the same values for the attributes (including :attr:`~django.db.models.ForeignKey.related_name`) each time. 859 859 860 860 To work around this problem, when you are using :attr:`~django.db.models.ForeignKey.related_name` in an 861 abstract base class (only), part of the name should be the string 862 ``'%(class)s'``. This is replaced by the lower-cased name of the child class 863 that the field is used in. Since each class has a different name, each related 864 name will end up being different. For example:: 865 861 abstract base class (only), part of the name should be contain 862 ``'%(app_label)s'`` and ``'%(class)s'``. ``'%(class)s'`` is replaced by the lower-cased name of the child class 863 that the field is used in. ``'%(app_label)s'`` is replaced by the lower-cased name of the app the child class 864 is contained within. Each installed application name must be unique and the model class names within each app 865 must also be unique, therefore the resulting name will end up being different. 866 For example, given an app ``common/models.py``:: 866 867 class Base(models.Model): 867 m2m = models.ManyToMany(OtherModel, related_name="%( class)s_related")868 m2m = models.ManyToMany(OtherModel, related_name="%(app_label)s_%(class)s_related") 868 869 869 870 class Meta: 870 871 abstract = True … … 875 876 class ChildB(Base): 876 877 pass 877 878 878 The reverse name of the ``ChildA.m2m`` field will be ``childa_related``, 879 whilst the reverse name of the ``ChildB.m2m`` field will be 880 ``childb_related``. It is up to you how you use the ``'%(class)s'`` portion to 881 construct your related name, but if you forget to use it, Django will raise 879 Along with another app ``rare/models.py``:: 880 from common.models import Base 881 882 class ChildB(Base): 883 pass 884 885 The reverse name of the ``commmon.ChildA.m2m`` field will be ``common_childa_related``, 886 whilst the reverse name of the ``common.ChildB.m2m`` field will be 887 ``common_childb_related``, and finally the reverse name of the ``rare.ChildB.m2m`` field will be 888 ``rare_childb_related``. It is up to you how you use the ``'%(class)s'`` and ``'%(app_label)s`` 889 portion to construct your related name, but if you forget to use it, Django will raise 882 890 errors when you validate your models (or run :djadmin:`syncdb`). 883 891 884 892 If you don't specify a :attr:`~django.db.models.ForeignKey.related_name` attribute for a field in an