Django

Code

Changeset 7762

Show
Ignore:
Timestamp:
06/26/08 02:04:18 (3 months ago)
Author:
mtredinnick
Message:

Fixed #7215 -- Create correct reverse-relation accessors when using abstract base classes. Patch from Joel Watts.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r7693 r7762  
    377377    Filip Wasilewski <filip.wasilewski@gmail.com> 
    378378    Dan Watson <http://theidioteque.net/> 
     379    Joel Watts <joel@joelwatts.com> 
    379380    Chris Wesseling <Chris.Wesseling@cwi.nl> 
    380381    James Wheare <django@sparemint.com> 
  • django/trunk/django/db/models/fields/related.py

    r7622 r7762  
    104104        if hasattr(sup, 'contribute_to_class'): 
    105105            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 
    106110        other = self.rel.to 
    107111        if isinstance(other, basestring): 
     
    109113        else: 
    110114            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()} 
    113115 
    114116    def set_attributes_from_rel(self): 
     
    120122        self.set_attributes_from_rel() 
    121123        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) 
    123126 
    124127    def get_db_prep_lookup(self, lookup_type, value): 
  • django/trunk/tests/modeltests/model_inheritance/models.py

    r7600 r7762  
    3838    class Meta: 
    3939        pass 
     40 
     41# 
     42# Abstract base classes with related models 
     43# 
     44 
     45class Post(models.Model): 
     46    title = models.CharField(max_length=50) 
     47 
     48class 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 
     58class Comment(Attachment): 
     59    is_spam = models.BooleanField() 
     60 
     61class Link(Attachment): 
     62    url = models.URLField() 
    4063 
    4164# 
     
    129152AttributeError: type object 'CommonInfo' has no attribute 'objects' 
    130153 
    131 # The Place/Restaurant/ItalianRestaurant models, on the other hand, all exist 
    132 # as independent models. However, the subclasses also have transparent access 
    133 # to the fields of their ancestors. 
     154# Create a Post 
     155>>> post = Post(title='Lorem Ipsum') 
     156>>> post.save() 
     157 
     158# The Post model has distinct 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 doesn't have an attribute called 'attached_%(class)s_set'. 
     165>>> getattr(post, 'attached_%(class)s_set') 
     166Traceback (most recent call last): 
     167    ... 
     168AttributeError: 'Post' object has no attribute 'attached_%(class)s_set' 
     169 
     170# The Place/Restaurant/ItalianRestaurant models all exist as independent 
     171# models. However, the subclasses also have transparent access to the fields of 
     172# their ancestors. 
    134173 
    135174# Create a couple of Places.