Skip to content

Commit 2e0d21a

Browse files
committed
fix QuerySet.update() with related model instance
1 parent 01a2ed1 commit 2e0d21a

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

.github/workflows/test-python.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ jobs:
7474
model_fields.test_datetimefield
7575
model_fields.test_decimalfield
7676
model_fields.test_charfield
77+
model_fields.test_floatfield
7778
model_fields.test_textfield
79+
model_fields.test_uuid.TestAsPrimaryKey.test_update_with_related_model_instance
7880
or_lookups
7981
8082
docs:

django_mongodb/compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ def execute_sql(self, result_type):
223223
self.pre_sql_setup()
224224
values = []
225225
for field, _, value in self.query.values:
226+
if hasattr(value, "prepare_database_save"):
227+
if field.remote_field:
228+
value = value.prepare_database_save(field)
229+
else:
230+
raise TypeError(
231+
f"Tried to update field {field} with a model "
232+
f"instance, {value!r}. Use a value compatible with "
233+
f"{field.__class__.__name__}."
234+
)
226235
prepared = field.get_db_prep_save(value, connection=self.connection)
227236
values.append((field, prepared))
228237
return self.update(values)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ignore = [
104104
"ARG002", # Unused method argument:
105105
"S101", # Use of `assert` detected
106106
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
107+
"PLW2901", # `for` loop variable `value` overwritten by assignment target
107108
]
108109
unfixable = []
109110
exclude = []

0 commit comments

Comments
 (0)