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/many_to_one_regress/models.py

    r7778 r8185  
    5656 
    5757# 
    58 # Tests of ForeignKey assignment and the related-object cache (see #6886) 
     58# Tests of ForeignKey assignment and the related-object cache (see #6886). 
    5959# 
    6060>>> p = Parent.objects.create(name="Parent") 
    6161>>> c = Child.objects.create(name="Child", parent=p) 
    6262 
    63 # Look up the object again so that we get a "fresh" object 
     63# Look up the object again so that we get a "fresh" object. 
    6464>>> c = Child.objects.get(name="Child") 
    6565>>> p = c.parent 
    6666 
    67 # Accessing the related object again returns the exactly same object 
     67# Accessing the related object again returns the exactly same object. 
    6868>>> c.parent is p 
    6969True 
    7070 
    71 # But if we kill the cache, we get a new object 
     71# But if we kill the cache, we get a new object. 
    7272>>> del c._parent_cache 
    7373>>> c.parent is p 
    7474False 
    7575 
    76 # Assigning a new object results in that object getting cached immediately 
     76# Assigning a new object results in that object getting cached immediately. 
    7777>>> p2 = Parent.objects.create(name="Parent 2") 
    7878>>> c.parent = p2 
     
    8080True 
    8181 
    82 # Assigning None fails: Child.parent is null=False 
     82# Assigning None succeeds if field is null=True. 
     83>>> p.bestchild = None 
     84>>> p.bestchild is None 
     85True 
     86 
     87# Assigning None fails: Child.parent is null=False. 
    8388>>> c.parent = None 
    8489Traceback (most recent call last): 
     
    9297ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance. 
    9398 
    94 # Test of multiple ForeignKeys to the same model (bug #7125) 
     99# Creation using keyword argument should cache the related object. 
     100>>> p = Parent.objects.get(name="Parent") 
     101>>> c = Child(parent=p) 
     102>>> c.parent is p 
     103True 
    95104 
     105# Creation using keyword argument and unsaved related instance (#8070). 
     106>>> p = Parent() 
     107>>> c = Child(parent=p) 
     108>>> c.parent is p 
     109True 
     110 
     111# Creation using attname keyword argument and an id will cause the related 
     112# object to be fetched. 
     113>>> p = Parent.objects.get(name="Parent") 
     114>>> c = Child(parent_id=p.id) 
     115>>> c.parent is p 
     116False 
     117>>> c.parent == p 
     118True 
     119 
     120 
     121# 
     122# Test of multiple ForeignKeys to the same model (bug #7125). 
     123# 
    96124>>> c1 = Category.objects.create(name='First') 
    97125>>> c2 = Category.objects.create(name='Second')