diff options
| author | Daniel Pyrathon <pirosb3@gmail.com> | 2014-03-10 15:17:57 +0000 |
|---|---|---|
| committer | Marc Tamlyn <marc.tamlyn@gmail.com> | 2014-03-10 15:25:18 +0000 |
| commit | 819e09b848e4f1cc45165a99ffbef1307b215a08 (patch) | |
| tree | b424ef7353b1cdaeb60adf166606bc05357b3acc /django | |
| parent | 3bd45ba00d0f43e9ea192eca132624b30cae4eee (diff) | |
Fixed #22210 -- Saving model instances to non-related fields.
Previously, saving a model instance to a non-related field (in
particular a FloatField) would silently convert the model to an Integer
(the pk) and save it. This is undesirable behaviour, and likely to cause
confusion so the validatio has been hardened.
Thanks to @PirosB3 for the patch and @jarshwah for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/sql/compiler.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 11c503cd5d..5636f344df 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -4,6 +4,7 @@ from django.conf import settings from django.core.exceptions import FieldError from django.db.backends.utils import truncate_name from django.db.models.constants import LOOKUP_SEP +from django.db.models.expressions import ExpressionNode from django.db.models.query_utils import select_related_descend, QueryWrapper from django.db.models.sql.constants import (CURSOR, SINGLE, MULTI, NO_RESULTS, ORDER_DIR, GET_ITERATOR_CHUNK_SIZE, SelectInfo) @@ -951,7 +952,14 @@ class SQLUpdateCompiler(SQLCompiler): values, update_params = [], [] for field, model, val in self.query.values: if hasattr(val, 'prepare_database_save'): - val = val.prepare_database_save(field) + if field.rel or isinstance(val, ExpressionNode): + val = val.prepare_database_save(field) + else: + raise TypeError("Database is trying to update a relational field " + "of type %s with a value of type %s. Make sure " + "you are setting the correct relations" % + (field.__class__.__name__, + val.__class__.__name__)) else: val = field.get_db_prep_save(val, connection=self.connection) |
