Django

Code

Show
Ignore:
Timestamp:
08/01/08 18:16:59 (4 months ago)
Author:
gwilson
Message:

Fixed #8070 -- Cache related objects passed to Model init as keyword arguments. Also:

  • Model init no longer performs a database query to refetch the related objects it is passed.
  • Model init now caches unsaved related objects correctly, too. (Previously, accessing the field would raise DoesNotExist error for null=False fields.)
  • Added tests for assigning None to null=True ForeignKey fields (refs #6886).
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/regressiontests/one_to_one_regress/models.py

    r7574 r8185  
    2323        return u"%s the bar" % self.place.name 
    2424 
     25class UndergroundBar(models.Model): 
     26    place = models.OneToOneField(Place, null=True) 
     27    serves_cocktails = models.BooleanField() 
     28 
    2529class Favorites(models.Model): 
    2630    name = models.CharField(max_length = 50) 
     
    4347[<Restaurant: Demon Dogs the restaurant>] 
    4448 
    45 # Regression test for #7173: Check that the name of the cache for the  
     49# Regression test for #7173: Check that the name of the cache for the 
    4650# reverse object is correct. 
    4751>>> b = Bar(place=p1, serves_cocktails=False) 
     
    5458# 
    5559# Regression test for #6886 (the related-object cache) 
    56 #  
     60# 
    5761 
    5862# Look up the objects again so that we get "fresh" objects 
     
    7781True 
    7882 
     83# Assigning None succeeds if field is null=True. 
     84>>> ug_bar = UndergroundBar.objects.create(place=p, serves_cocktails=False) 
     85>>> ug_bar.place = None 
     86>>> ug_bar.place is None 
     87True 
     88 
    7989# Assigning None fails: Place.restaurant is null=False 
    8090>>> p.restaurant = None 
     
    8999ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. 
    90100 
     101# Creation using keyword argument should cache the related object. 
     102>>> p = Place.objects.get(name="Demon Dogs") 
     103>>> r = Restaurant(place=p) 
     104>>> r.place is p 
     105True 
     106 
     107# Creation using keyword argument and unsaved related instance (#8070). 
     108>>> p = Place() 
     109>>> r = Restaurant(place=p) 
     110>>> r.place is p 
     111True 
     112 
     113# Creation using attname keyword argument and an id will cause the related 
     114# object to be fetched. 
     115>>> p = Place.objects.get(name="Demon Dogs") 
     116>>> r = Restaurant(place_id=p.id) 
     117>>> r.place is p 
     118False 
     119>>> r.place == p 
     120True 
     121 
    91122"""}