Changeset 8185
- Timestamp:
- 08/01/08 18:16:59 (4 months ago)
- Files:
-
- django/trunk/django/db/models/base.py (modified) (2 diffs)
- django/trunk/tests/modeltests/many_to_one/models.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/many_to_one_regress/models.py (modified) (3 diffs)
- django/trunk/tests/regressiontests/one_to_one_regress/models.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/base.py
r8046 r8185 201 201 202 202 for field in fields_iter: 203 rel_obj = None 203 204 if kwargs: 204 205 if isinstance(field.rel, ManyToOneRel): … … 217 218 if rel_obj is None and field.null: 218 219 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)))225 220 else: 226 221 val = kwargs.pop(field.attname, field.get_default()) 227 222 else: 228 223 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) 230 232 231 233 if kwargs: django/trunk/tests/modeltests/many_to_one/models.py
r7971 r8185 47 47 # Article objects have access to their related Reporter objects. 48 48 >>> r = a.reporter 49 50 # These are strings instead of unicode strings because that's what was used in 51 # the creation of this reporter (and we haven't refreshed the data from the 52 # database, which always returns unicode strings). 49 53 >>> r.first_name, r.last_name 50 ( u'John', u'Smith')54 ('John', 'Smith') 51 55 52 56 # Create an Article via the Reporter object. … … 177 181 178 182 # 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(), 183 # The queryset must be reduced to a list of values using values(), 180 184 # then converted into a query 181 185 >>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John').values('pk').query).distinct() django/trunk/tests/regressiontests/many_to_one_regress/models.py
r7778 r8185 56 56 57 57 # 58 # Tests of ForeignKey assignment and the related-object cache (see #6886) 58 # Tests of ForeignKey assignment and the related-object cache (see #6886). 59 59 # 60 60 >>> p = Parent.objects.create(name="Parent") 61 61 >>> c = Child.objects.create(name="Child", parent=p) 62 62 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. 64 64 >>> c = Child.objects.get(name="Child") 65 65 >>> p = c.parent 66 66 67 # Accessing the related object again returns the exactly same object 67 # Accessing the related object again returns the exactly same object. 68 68 >>> c.parent is p 69 69 True 70 70 71 # But if we kill the cache, we get a new object 71 # But if we kill the cache, we get a new object. 72 72 >>> del c._parent_cache 73 73 >>> c.parent is p 74 74 False 75 75 76 # Assigning a new object results in that object getting cached immediately 76 # Assigning a new object results in that object getting cached immediately. 77 77 >>> p2 = Parent.objects.create(name="Parent 2") 78 78 >>> c.parent = p2 … … 80 80 True 81 81 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 85 True 86 87 # Assigning None fails: Child.parent is null=False. 83 88 >>> c.parent = None 84 89 Traceback (most recent call last): … … 92 97 ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance. 93 98 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 103 True 95 104 105 # Creation using keyword argument and unsaved related instance (#8070). 106 >>> p = Parent() 107 >>> c = Child(parent=p) 108 >>> c.parent is p 109 True 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 116 False 117 >>> c.parent == p 118 True 119 120 121 # 122 # Test of multiple ForeignKeys to the same model (bug #7125). 123 # 96 124 >>> c1 = Category.objects.create(name='First') 97 125 >>> c2 = Category.objects.create(name='Second') django/trunk/tests/regressiontests/one_to_one_regress/models.py
r7574 r8185 23 23 return u"%s the bar" % self.place.name 24 24 25 class UndergroundBar(models.Model): 26 place = models.OneToOneField(Place, null=True) 27 serves_cocktails = models.BooleanField() 28 25 29 class Favorites(models.Model): 26 30 name = models.CharField(max_length = 50) … … 43 47 [<Restaurant: Demon Dogs the restaurant>] 44 48 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 46 50 # reverse object is correct. 47 51 >>> b = Bar(place=p1, serves_cocktails=False) … … 54 58 # 55 59 # Regression test for #6886 (the related-object cache) 56 # 60 # 57 61 58 62 # Look up the objects again so that we get "fresh" objects … … 77 81 True 78 82 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 87 True 88 79 89 # Assigning None fails: Place.restaurant is null=False 80 90 >>> p.restaurant = None … … 89 99 ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. 90 100 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 105 True 106 107 # Creation using keyword argument and unsaved related instance (#8070). 108 >>> p = Place() 109 >>> r = Restaurant(place=p) 110 >>> r.place is p 111 True 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 118 False 119 >>> r.place == p 120 True 121 91 122 """}
