Django

Code

Show
Ignore:
Timestamp:
06/04/08 19:39:32 (3 months ago)
Author:
jacob
Message:

Fixed #6886: Tightened up ForeignKey? and OneToOne? field assignment. Specifically:

  • Raise a ValueError? if you try to assign the wrong type of object.
  • Raise a ValueError? if you try to assign None to a field not specified with null=True.
  • Cache the set value at set time instead of just at lookup time.

This is a slightly backwards-incompatible change; see BackwardsIncompatibleChanges for more details.

Files:

Legend:

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

    r7561 r7574  
    5151>>> p1.bar 
    5252<Bar: Demon Dogs the bar> 
     53 
     54# 
     55# Regression test for #6886 (the related-object cache) 
     56#  
     57 
     58# Look up the objects again so that we get "fresh" objects 
     59>>> p = Place.objects.get(name="Demon Dogs") 
     60>>> r = p.restaurant 
     61 
     62# Accessing the related object again returns the exactly same object 
     63>>> p.restaurant is r 
     64True 
     65 
     66# But if we kill the cache, we get a new object 
     67>>> del p._restaurant_cache 
     68>>> p.restaurant is r 
     69False 
     70 
     71# Reassigning the Restaurant object results in an immediate cache update 
     72# We can't use a new Restaurant because that'll violate one-to-one, but 
     73# with a new *instance* the is test below will fail if #6886 regresses. 
     74>>> r2 = Restaurant.objects.get(pk=r.pk) 
     75>>> p.restaurant = r2 
     76>>> p.restaurant is r2 
     77True 
     78 
     79# Assigning None fails: Place.restaurant is null=False 
     80>>> p.restaurant = None 
     81Traceback (most recent call last): 
     82    ... 
     83ValueError: Cannot assign None: "Place.restaurant" does not allow null values. 
     84 
     85# You also can't assign an object of the wrong type here 
     86>>> p.restaurant = p 
     87Traceback (most recent call last): 
     88    ... 
     89ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. 
     90 
    5391"""}