mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #25160 -- Moved unsaved model instance data loss check to Model.save()
This mostly reverts5643a3b51b
and81e1a35c36
. Thanks Carl Meyer for review.
This commit is contained in:
parent
12f91f6ebd
commit
5980b05c1f
15 changed files with 106 additions and 198 deletions
|
@ -1383,30 +1383,6 @@ The possible values for :attr:`~ForeignKey.on_delete` are found in
|
|||
|
||||
If in doubt, leave it to its default of ``True``.
|
||||
|
||||
.. attribute:: ForeignKey.allow_unsaved_instance_assignment
|
||||
|
||||
.. versionadded:: 1.8
|
||||
|
||||
This flag was added for backwards compatibility as older versions of
|
||||
Django always allowed assigning unsaved model instances.
|
||||
|
||||
Django prevents unsaved model instances from being assigned to a
|
||||
``ForeignKey`` field to prevent accidental data loss (unsaved foreign keys
|
||||
are silently ignored when saving a model instance).
|
||||
|
||||
If you require allowing the assignment of unsaved instances and aren't
|
||||
concerned about the data loss possibility (e.g. you never save the objects
|
||||
to the database), you can disable this check by creating a subclass of the
|
||||
field class and setting its ``allow_unsaved_instance_assignment`` attribute
|
||||
to ``True``. For example::
|
||||
|
||||
class UnsavedForeignKey(models.ForeignKey):
|
||||
# A ForeignKey which can point to an unsaved object
|
||||
allow_unsaved_instance_assignment = True
|
||||
|
||||
class Book(models.Model):
|
||||
author = UnsavedForeignKey(Author, on_delete=models.CASCADE)
|
||||
|
||||
.. _ref-manytomany:
|
||||
|
||||
``ManyToManyField``
|
||||
|
@ -1607,12 +1583,6 @@ that control how the relationship functions.
|
|||
|
||||
If in doubt, leave it to its default of ``True``.
|
||||
|
||||
.. attribute:: ManyToManyField.allow_unsaved_instance_assignment
|
||||
|
||||
.. versionadded:: 1.8
|
||||
|
||||
Works analogously to :attr:`ForeignKey.allow_unsaved_instance_assignment`.
|
||||
|
||||
:class:`ManyToManyField` does not support :attr:`~Field.validators`.
|
||||
|
||||
:attr:`~Field.null` has no effect since there is no way to require a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue