Skip to content

Commit

Permalink
fix: bulk_update when using models with custom fields (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatwolke authored Apr 29, 2024
1 parent 986e1b2 commit 345af17
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Added
Fixed
^^^^^
- Fix `DatetimeField` use '__year' report `'int' object has no attribute 'utcoffset'`. (#1575)
- Fix `bulk_update` when using custom fields. (#1564)

0.20.1
------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Contributors
* Stanislav Zmiev ``@Ovsyanka83``
* Waket Zheng ``@waketzheng``
* Yuval Ben-Arie ``@yuvalbenarie``
* Stephan Klein ``@privatwolke``

Special Thanks
==============
Expand Down
17 changes: 17 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
from pypika.terms import Function

from tests.testmodels import (
Currency,
DatetimeFields,
DefaultUpdate,
EnumFields,
Event,
IntFields,
JSONFields,
Service,
SmallIntFields,
Tournament,
UUIDFields,
Expand Down Expand Up @@ -90,6 +93,20 @@ async def test_bulk_update_smallint_none(self):
self.assertEqual((await SmallIntFields.get(pk=objs[0].pk)).smallintnum_null, None)
self.assertEqual((await SmallIntFields.get(pk=objs[1].pk)).smallintnum_null, None)

async def test_bulk_update_custom_field(self):
objs = [
await EnumFields.create(service=Service.python_programming, currency=Currency.EUR),
await EnumFields.create(service=Service.database_design, currency=Currency.USD),
]
objs[0].currency = Currency.USD
objs[1].service = Service.system_administration
rows_affected = await EnumFields.bulk_update(objs, fields=["service", "currency"])
self.assertEqual(rows_affected, 2)
self.assertEqual((await EnumFields.get(pk=objs[0].pk)).currency, Currency.USD)
self.assertEqual(
(await EnumFields.get(pk=objs[1].pk)).service, Service.system_administration
)

async def test_update_auto_now(self):
obj = await DefaultUpdate.create()

Expand Down
2 changes: 1 addition & 1 deletion tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ def _make_query(self) -> None:
pk_list = []
for obj in objects_item:
value = executor.column_map[pk_attr](obj.pk, None)
field_value = getattr(obj, field)
field_value = obj._meta.fields_map[field].to_db_value(getattr(obj, field), obj)
case.when(
pk == value,
(
Expand Down

0 comments on commit 345af17

Please sign in to comment.