diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-05-06 13:57:20 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-05-16 08:13:57 +0200 |
| commit | e03e5c751c56db5f4cb99e142c92d7d8db3a5463 (patch) | |
| tree | 474e4baf6ac38c2e940552a167bc8562eae142b3 /docs | |
| parent | 0b2ed4f7c8396c8d9aa8428a40e6b25c31312889 (diff) | |
Fixed #33312 -- Raised explicit exception when copying deferred model instances.
Previously save() would crash with an attempted forced update message, and both
save(force_insert=True) and bulk_create() would crash with DoesNotExist errors
trying to retrieve rows with an empty primary key (id IS NULL).
Implementing deferred field model instance copying might be doable in certain
cases (e.g. when all the deferred fields are db generated) but that's not
trivial to implement in a backward compatible way.
Thanks Adam Sołtysik for the report and test and Clifford for the review.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/db/queries.txt | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index eb5b323abb..bd092f524b 100644 --- a/docs/topics/db/queries.txt +++ b/docs/topics/db/queries.txt @@ -1590,6 +1590,21 @@ For example, assuming ``entry`` is already duplicated as above:: detail.entry = entry detail.save() +Note that it is not possible to copy instances of models with deferred fields +using this pattern unless values are assigned to them: + +.. code-block:: pycon + + >>> blog = Blog.objects.defer("name")[0] + >>> blog.pk = None + >>> blog._state.adding = True + >>> blog.save() + Traceback (most recent call last): + ... + AttributeError: Cannot retrieve deferred field 'name' from an unsaved model. + >>> blog.name = "Another Blog" + >>> blog.save() + .. _topics-db-queries-update: Updating multiple objects at once |
