=== modified file 'django/db/models/base.py'
|
|
|
200 | 200 | # keywords, or default. |
201 | 201 | |
202 | 202 | for field in fields_iter: |
| 203 | rel_obj = None |
203 | 204 | if kwargs: |
204 | 205 | if isinstance(field.rel, ManyToOneRel): |
205 | 206 | try: |
… |
… |
|
216 | 217 | # pass in "None" for related objects if it's allowed. |
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: |
232 | 234 | for prop in kwargs.keys(): |
=== modified file 'tests/modeltests/many_to_one/models.py'
|
|
|
46 | 46 | |
47 | 47 | # Article objects have access to their related Reporter objects. |
48 | 48 | >>> r = a.reporter |
| 49 | # These are strings instead of unicode strings because that's what was used in |
| 50 | # for the creation of this reporter (and we haven't refreshed the data from the |
| 51 | # database, which always returns unicode strings). |
49 | 52 | >>> r.first_name, r.last_name |
50 | | (u'John', u'Smith') |
| 53 | ('John', 'Smith') |
51 | 54 | |
52 | 55 | # Create an Article via the Reporter object. |
53 | 56 | >>> new_article = r.article_set.create(headline="John's second story", pub_date=datetime(2005, 7, 29)) |
=== modified file 'tests/regressiontests/many_to_one_regress/models.py'
|
|
|
55 | 55 | <Child: Child object> |
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 |
79 | 79 | >>> c.parent is p2 |
… |
… |
|
91 | 91 | ... |
92 | 92 | ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance. |
93 | 93 | |
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 |
| 98 | True |
| 99 | |
| 100 | # Creation using keyword argument and unsaved related instance (#8070). |
| 101 | >>> p = Parent() |
| 102 | >>> c = Child(parent=p) |
| 103 | >>> c.parent is p |
| 104 | True |
| 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 |
| 111 | False |
| 112 | >>> c.parent == p |
| 113 | True |
| 114 | |
| 115 | |
| 116 | # |
| 117 | # Test of multiple ForeignKeys to the same model (bug #7125). |
| 118 | # |
96 | 119 | >>> c1 = Category.objects.create(name='First') |
97 | 120 | >>> c2 = Category.objects.create(name='Second') |
98 | 121 | >>> c3 = Category.objects.create(name='Third') |
=== modified file 'tests/regressiontests/one_to_one_regress/models.py'
|
|
|
88 | 88 | ... |
89 | 89 | ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. |
90 | 90 | |
| 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 |
| 95 | True |
| 96 | |
| 97 | # Creation using keyword argument and unsaved related instance (#8070). |
| 98 | >>> p = Place() |
| 99 | >>> r = Restaurant(place=p) |
| 100 | >>> r.place is p |
| 101 | True |
| 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 |
| 108 | False |
| 109 | >>> r.place == p |
| 110 | True |
| 111 | |
91 | 112 | """} |