Ticket #7215: related_name.diff
File related_name.diff, 3.2 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/related.py
103 103 104 104 if hasattr(sup, 'contribute_to_class'): 105 105 sup.contribute_to_class(cls, name) 106 107 if not cls._meta.abstract and self.rel.related_name: 108 self.rel.related_name = self.rel.related_name % {'class': cls.__name__.lower()} 109 106 110 other = self.rel.to 107 111 if isinstance(other, basestring): 108 112 add_lazy_relation(cls, self, other) 109 113 else: 110 114 self.do_related_class(other, cls) 111 if not cls._meta.abstract and self.rel.related_name:112 self.rel.related_name = self.rel.related_name % {'class': cls.__name__.lower()}113 115 114 116 def set_attributes_from_rel(self): 115 117 self.name = self.name or (self.rel.to._meta.object_name.lower() + '_' + self.rel.to._meta.pk.name) … … 119 121 def do_related_class(self, other, cls): 120 122 self.set_attributes_from_rel() 121 123 related = RelatedObject(other, cls, self) 122 self.contribute_to_related_class(other, related) 124 if not cls._meta.abstract: 125 self.contribute_to_related_class(other, related) 123 126 124 127 def get_db_prep_lookup(self, lookup_type, value): 125 128 # If we are doing a lookup on a Related Field, we must be -
tests/modeltests/model_inheritance/models.py
39 39 pass 40 40 41 41 # 42 # Abstract base classes with related models 43 # 44 45 class Post(models.Model): 46 title = models.CharField(max_length=50) 47 48 class Attachment(models.Model): 49 post = models.ForeignKey(Post, related_name='attached_%(class)s_set') 50 content = models.TextField() 51 52 class Meta: 53 abstract = True 54 55 def __unicode__(self): 56 return self.content 57 58 class Comment(Attachment): 59 is_spam = models.BooleanField() 60 61 class Link(Attachment): 62 url = models.URLField() 63 64 # 42 65 # Multi-table inheritance 43 66 # 44 67 … … 128 151 ... 129 152 AttributeError: type object 'CommonInfo' has no attribute 'objects' 130 153 154 # Create a Post 155 >>> post = Post(title='Lorem Ipsum') 156 >>> post.save() 157 158 # The Post model should have accessors for the Comment and Link models 159 >>> post.attached_comment_set.create(content='Save $ on V1agr@', is_spam=True) 160 <Comment: Save $ on V1agr@> 161 >>> post.attached_link_set.create(content='The Web framework for perfectionists with deadlines.', url='http://www.djangoproject.com/') 162 <Link: The Web framework for perfectionists with deadlines.> 163 164 # The Post model should not have an attribute 'attached_%(class)s_set' 165 >>> getattr(post, 'attached_%(class)s_set') 166 Traceback (most recent call last): 167 ... 168 AttributeError: 'Post' object has no attribute 'attached_%(class)s_set' 169 131 170 # The Place/Restaurant/ItalianRestaurant models, on the other hand, all exist 132 171 # as independent models. However, the subclasses also have transparent access 133 172 # to the fields of their ancestors.