diff options
| author | Karl Hobley <karlhobley10@gmail.com> | 2015-03-16 19:28:53 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-03-18 19:00:09 -0400 |
| commit | 81e1a35c364e5353d2bf99368ad30a4184fbb653 (patch) | |
| tree | 2cbfcc6a605600d4f4c387d4c49d17dfe300b91f /docs | |
| parent | 02d78bb1a80706d941ffc6c892cc75208eb6b782 (diff) | |
Fixed #24495 -- Allowed unsaved model instance assignment check to be bypassed.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/contrib/contenttypes.txt | 7 | ||||
| -rw-r--r-- | docs/ref/models/fields.txt | 32 | ||||
| -rw-r--r-- | docs/releases/1.8.txt | 6 | ||||
| -rw-r--r-- | docs/topics/db/examples/many_to_one.txt | 4 | ||||
| -rw-r--r-- | docs/topics/db/examples/one_to_one.txt | 4 |
5 files changed, 52 insertions, 1 deletions
diff --git a/docs/ref/contrib/contenttypes.txt b/docs/ref/contrib/contenttypes.txt index f741e30a37..ef50f84ce8 100644 --- a/docs/ref/contrib/contenttypes.txt +++ b/docs/ref/contrib/contenttypes.txt @@ -299,6 +299,13 @@ model: is ``True``. This mirrors the ``for_concrete_model`` argument to :meth:`~django.contrib.contenttypes.models.ContentTypeManager.get_for_model`. + .. attribute:: GenericForeignKey.allow_unsaved_instance_assignment + + .. versionadded:: 1.8 + + Works analogously to :attr:`ForeignKey.allow_unsaved_instance_assignment + <django.db.models.ForeignKey.allow_unsaved_instance_assignment>`. + .. admonition:: Primary key type compatibility The "object_id" field doesn't have to be the same type as the diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index d1fc04d3b5..9fd9bd7fa3 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -1291,6 +1291,30 @@ 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) + .. _ref-manytomany: ``ManyToManyField`` @@ -1388,7 +1412,7 @@ that control how the relationship functions. * ``<other_model>_id``: the ``id`` of the model that the ``ManyToManyField`` points to. - If the ``ManyToManyField`` points from and to the same model, the following + If the ``ManyToManyField`` points from and to the same model, the following fields are generated: * ``id``: the primary key of the relation. @@ -1483,6 +1507,12 @@ 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 diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index bbbe97a881..a0a27ee89e 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -713,6 +713,12 @@ Now, an error will be raised to prevent data loss:: ... ValueError: Cannot assign "<Author: John>": "Author" instance isn't saved in the database. +If you require allowing the assignment of unsaved instances (the old behavior) +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 using the +:attr:`~django.db.models.ForeignKey.allow_unsaved_instance_assignment` +attribute. + Management commands that only accept positional arguments ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/db/examples/many_to_one.txt b/docs/topics/db/examples/many_to_one.txt index c5c3e10034..e43809fd1c 100644 --- a/docs/topics/db/examples/many_to_one.txt +++ b/docs/topics/db/examples/many_to_one.txt @@ -60,6 +60,10 @@ raises ``ValueError``:: ... ValueError: 'Cannot assign "<Reporter: John Smith>": "Reporter" instance isn't saved in the database.' +If you want to disable the unsaved instance check, you can use the +:attr:`~django.db.models.ForeignKey.allow_unsaved_instance_assignment` +attribute. + .. versionchanged:: 1.8 Previously, assigning unsaved objects did not raise an error and could diff --git a/docs/topics/db/examples/one_to_one.txt b/docs/topics/db/examples/one_to_one.txt index f1bdf3de9c..f2a3f54508 100644 --- a/docs/topics/db/examples/one_to_one.txt +++ b/docs/topics/db/examples/one_to_one.txt @@ -101,6 +101,10 @@ raises ``ValueError``:: ... ValueError: 'Cannot assign "<Restaurant: Demon Dogs the restaurant>": "Restaurant" instance isn't saved in the database.' +If you want to disable the unsaved instance check, you can use the +:attr:`~django.db.models.ForeignKey.allow_unsaved_instance_assignment` +attribute. + .. versionchanged:: 1.8 Previously, assigning unsaved objects did not raise an error and could |
