mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #10811 -- Made assigning unsaved objects to FK, O2O, and GFK raise ValueError.
This prevents silent data loss. Thanks Aymeric Augustin for the initial patch and Loic Bistuer for the review.
This commit is contained in:
parent
4f72e5f03a
commit
5643a3b51b
12 changed files with 141 additions and 76 deletions
|
@ -52,6 +52,21 @@ Create an Article::
|
|||
>>> a.reporter
|
||||
<Reporter: John Smith>
|
||||
|
||||
Note that you must save an object before it can be assigned to a foreign key
|
||||
relationship. For example, creating an ``Article`` with unsaved ``Reporter``
|
||||
raises ``ValueError``::
|
||||
|
||||
>>> r3 = Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
||||
>>> Article(headline="This is a test", pub_date=date(2005, 7, 27), reporter=r3)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: 'Cannot assign "<Reporter: John Smith>": "Reporter" instance isn't saved in the database.'
|
||||
|
||||
.. versionchanged:: 1.8
|
||||
|
||||
Previously, assigning unsaved objects did not raise an error and could
|
||||
result in silent data loss.
|
||||
|
||||
Article objects have access to their related Reporter objects::
|
||||
|
||||
>>> r = a.reporter
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue