Ticket #8070: 8070.2.patch

File 8070.2.patch, 6.4 KB (added by Gary Wilson, 16 years ago)

corrected a comment

  • django/db/models/base.py

    === modified file 'django/db/models/base.py'
     
    200200        # keywords, or default.
    201201
    202202        for field in fields_iter:
     203            rel_obj = None
    203204            if kwargs:
    204205                if isinstance(field.rel, ManyToOneRel):
    205206                    try:
     
    216217                        # pass in "None" for related objects if it's allowed.
    217218                        if rel_obj is None and field.null:
    218219                            val = None
    219                         else:
    220                             try:
    221                                 val = getattr(rel_obj, field.rel.get_related_field().attname)
    222                             except AttributeError:
    223                                 raise TypeError("Invalid value: %r should be a %s instance, not a %s" %
    224                                     (field.name, field.rel.to, type(rel_obj)))
    225220                else:
    226221                    val = kwargs.pop(field.attname, field.get_default())
    227222            else:
    228223                val = field.get_default()
    229             setattr(self, field.attname, val)
     224            # If we got passed a related instance, set it using the field.name
     225            # instead of field.attname (e.g. "user" instead of "user_id") so
     226            # that the object gets properly cached (and type checked) by the
     227            # RelatedObjectDescriptor.
     228            if rel_obj:
     229                setattr(self, field.name, rel_obj)
     230            else:
     231                setattr(self, field.attname, val)
    230232
    231233        if kwargs:
    232234            for prop in kwargs.keys():
  • tests/modeltests/many_to_one/models.py

    === modified file 'tests/modeltests/many_to_one/models.py'
     
    4646
    4747# Article objects have access to their related Reporter objects.
    4848>>> r = a.reporter
     49# These are strings instead of unicode strings because that's what was used in
     50# the creation of this reporter (and we haven't refreshed the data from the
     51# database, which always returns unicode strings).
    4952>>> r.first_name, r.last_name
    50 (u'John', u'Smith')
     53('John', 'Smith')
    5154
    5255# Create an Article via the Reporter object.
    5356>>> new_article = r.article_set.create(headline="John's second story", pub_date=datetime(2005, 7, 29))
     
    176179[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]
    177180
    178181# You can also use a queryset instead of a literal list of instances.
    179 # The queryset must be reduced to a list of values using values(), 
     182# The queryset must be reduced to a list of values using values(),
    180183# then converted into a query
    181184>>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John').values('pk').query).distinct()
    182185[<Article: John's second story>, <Article: This is a test>]
  • tests/regressiontests/many_to_one_regress/models.py

    === modified file 'tests/regressiontests/many_to_one_regress/models.py'
     
    5555<Child: Child object>
    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
    7979>>> c.parent is p2
     
    9191    ...
    9292ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance.
    9393
    94 # Test of multiple ForeignKeys to the same model (bug #7125)
    95 
     94# Creation using keyword argument should cache the related object.
     95>>> p = Parent.objects.get(name="Parent")
     96>>> c = Child(parent=p)
     97>>> c.parent is p
     98True
     99
     100# Creation using keyword argument and unsaved related instance (#8070).
     101>>> p = Parent()
     102>>> c = Child(parent=p)
     103>>> c.parent is p
     104True
     105
     106# Creation using attname keyword argument and an id will cause the related
     107# object to be fetched.
     108>>> p = Parent.objects.get(name="Parent")
     109>>> c = Child(parent_id=p.id)
     110>>> c.parent is p
     111False
     112>>> c.parent == p
     113True
     114
     115
     116#
     117# Test of multiple ForeignKeys to the same model (bug #7125).
     118#
    96119>>> c1 = Category.objects.create(name='First')
    97120>>> c2 = Category.objects.create(name='Second')
    98121>>> c3 = Category.objects.create(name='Third')
  • tests/regressiontests/one_to_one_regress/models.py

    === modified file 'tests/regressiontests/one_to_one_regress/models.py'
     
    4242>>> f.restaurants.all()
    4343[<Restaurant: Demon Dogs the restaurant>]
    4444
    45 # Regression test for #7173: Check that the name of the cache for the 
     45# Regression test for #7173: Check that the name of the cache for the
    4646# reverse object is correct.
    4747>>> b = Bar(place=p1, serves_cocktails=False)
    4848>>> b.save()
     
    5353
    5454#
    5555# Regression test for #6886 (the related-object cache)
    56 # 
     56#
    5757
    5858# Look up the objects again so that we get "fresh" objects
    5959>>> p = Place.objects.get(name="Demon Dogs")
     
    8888    ...
    8989ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance.
    9090
     91# Creation using keyword argument should cache the related object.
     92>>> p = Place.objects.get(name="Demon Dogs")
     93>>> r = Restaurant(place=p)
     94>>> r.place is p
     95True
     96
     97# Creation using keyword argument and unsaved related instance (#8070).
     98>>> p = Place()
     99>>> r = Restaurant(place=p)
     100>>> r.place is p
     101True
     102
     103# Creation using attname keyword argument and an id will cause the related
     104# object to be fetched.
     105>>> p = Place.objects.get(name="Demon Dogs")
     106>>> r = Restaurant(place_id=p.id)
     107>>> r.place is p
     108False
     109>>> r.place == p
     110True
     111
    91112"""}
Back to Top