Django

Code

Changeset 4676

Show
Ignore:
Timestamp:
03/07/07 21:21:35 (2 years ago)
Author:
mtredinnick
Message:

Fixed #1839, #2415, #2536 -- Fixed a generated name clash that was common in
self-referential and circular relations. A lot of community debugging went into
this fix, so thanks to bmurdock@gmail.com, Marek Kubica, ramiro, Michael
Radziej (the last two giving test cases showing the problem) and James Bennett
(who did the hard work to actually diagnose the true problem and fix it).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/related.py

    r4542 r4676  
    22    def __init__(self, related_object, field_mapping, original): 
    33        self.relation = related_object 
    4         self.field_mappings = field_mapping[related_object.opts.module_name] 
     4        self.field_mappings = field_mapping[related_object.name] 
    55 
    66    def template_name(self): 
     
    1717        self.field = field 
    1818        self.edit_inline = field.rel.edit_inline 
    19         self.name = self.opts.module_name 
     19        self.name = '%s_%s' % (self.opts.app_label, self.opts.module_name) 
    2020        self.var_name = self.opts.object_name.lower() 
    2121 
  • django/trunk/tests/regressiontests/many_to_one_regress/models.py

    r3661 r4676  
    11from django.db import models 
     2 
     3# If ticket #1578 ever slips back in, these models will not be able to be 
     4# created (the field names being lower-cased versions of their opposite 
     5# classes is important here). 
    26 
    37class First(models.Model): 
     
    711    first = models.ForeignKey(First, related_name = 'the_first') 
    812 
    9 # If ticket #1578 ever slips back in, these models will not be able to be 
    10 # created (the field names being lower-cased versions of their opposite 
    11 # classes is important here). 
     13# Protect against repetition of #1839, #2415 and #2536. 
     14class Third(models.Model): 
     15    name = models.CharField(maxlength=20) 
     16    third = models.ForeignKey('self', null=True, related_name='child_set') 
    1217 
    13 __test__ = {'API_TESTS':""} 
     18class Parent(models.Model): 
     19    name = models.CharField(maxlength=20) 
     20    bestchild = models.ForeignKey('Child', null=True, related_name='favored_by') 
     21 
     22class Child(models.Model): 
     23    name = models.CharField(maxlength=20) 
     24    parent = models.ForeignKey(Parent) 
     25 
     26 
     27__test__ = {'API_TESTS':""" 
     28>>> Third.AddManipulator().save(dict(id='3', name='An example', another=None))  
     29<Third: Third object> 
     30>>> parent = Parent(name = 'fred') 
     31>>> parent.save() 
     32>>> Child.AddManipulator().save(dict(name='bam-bam', parent=parent.id)) 
     33<Child: Child object> 
     34"""}