Django

Code

Changeset 2564

Show
Ignore:
Timestamp:
03/25/06 19:37:06 (3 years ago)
Author:
russellm
Message:

magic-removal: Fixed assignment notation on descriptors for one-to-one fields.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/models/fields/related.py

    r2552 r2564  
    9898            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name 
    9999        # Set the value of the related field 
    100         setattr(value, self.related.field.attname, instance) 
    101              
     100        setattr(value, self.related.field.rel.get_related_field().attname, instance) 
     101 
     102        # Set the cache on the provided object to point to the new object 
     103        setattr(value, self.related.field.get_cache_name(), instance) 
     104 
    102105class ReverseSingleRelatedObjectDescriptor(object): 
    103106    # This class provides the functionality that makes the related-object 
     
    143146        # Set the cache to point to the new object 
    144147        setattr(instance, self.field.get_cache_name(), value) 
    145          
     148 
    146149class ForeignRelatedObjectsDescriptor(object): 
    147150    # This class provides the functionality that makes the related-object 
  • django/branches/magic-removal/tests/modeltests/one_to_one/models.py

    r2437 r2564  
    5656DoesNotExist: Restaurant does not exist for {'place__pk': ...} 
    5757 
     58# Set the place using assignment notation. Because place is the primary key on Restaurant, 
     59# the save will create a new restaurant 
     60>>> r.place = p2 
     61>>> r.save() 
     62>>> p2.restaurant 
     63Ace Hardware the restaurant 
     64>>> r.place 
     65Ace Hardware the place 
     66 
     67# Set the place back again, using assignment in the reverse direction 
     68# Need to reget restaurant object first, because the reverse set  
     69# can't update the existing restaurant instance 
     70>>> p1.restaurant = r 
     71>>> r.save() 
     72>>> p1.restaurant 
     73Demon Dogs the restaurant 
     74 
     75>>> r = Restaurant.objects.get(pk=1) 
     76>>> r.place 
     77Demon Dogs the place 
     78 
    5879# Restaurant.objects.all() just returns the Restaurants, not the Places. 
     80# Note that there are two restaurants - Ace Hardware the Restaurant was created 
     81# in the call to r.place = p2. This means there are multiple restaurants referencing  
     82# a single place... 
    5983>>> Restaurant.objects.all() 
    60 [Demon Dogs the restaurant
     84[Demon Dogs the restaurant, Ace Hardware the restaurant
    6185 
    6286# Place.objects.all() returns all Places, regardless of whether they have