mirror of
https://github.com/django/django.git
synced 2025-09-02 16:57:36 +00:00
Fixed #23646 -- Added QuerySet.bulk_update() to efficiently update many models.
This commit is contained in:
parent
7b159df942
commit
9cbdb44014
11 changed files with 359 additions and 8 deletions
34
tests/postgres_tests/test_bulk_update.py
Normal file
34
tests/postgres_tests/test_bulk_update.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
from datetime import date
|
||||
|
||||
from . import PostgreSQLTestCase
|
||||
from .models import (
|
||||
HStoreModel, IntegerArrayModel, JSONModel, NestedIntegerArrayModel,
|
||||
NullableIntegerArrayModel, OtherTypesArrayModel, RangesModel,
|
||||
)
|
||||
|
||||
try:
|
||||
from psycopg2.extras import NumericRange, DateRange
|
||||
except ImportError:
|
||||
pass # psycopg2 isn't installed.
|
||||
|
||||
|
||||
class BulkSaveTests(PostgreSQLTestCase):
|
||||
def test_bulk_update(self):
|
||||
test_data = [
|
||||
(IntegerArrayModel, 'field', [], [1, 2, 3]),
|
||||
(NullableIntegerArrayModel, 'field', [1, 2, 3], None),
|
||||
(JSONModel, 'field', {'a': 'b'}, {'c': 'd'}),
|
||||
(NestedIntegerArrayModel, 'field', [], [[1, 2, 3]]),
|
||||
(HStoreModel, 'field', {}, {1: 2}),
|
||||
(RangesModel, 'ints', None, NumericRange(lower=1, upper=10)),
|
||||
(RangesModel, 'dates', None, DateRange(lower=date.today(), upper=date.today())),
|
||||
(OtherTypesArrayModel, 'ips', [], ['1.2.3.4']),
|
||||
(OtherTypesArrayModel, 'json', [], [{'a': 'b'}])
|
||||
]
|
||||
for Model, field, initial, new in test_data:
|
||||
with self.subTest(model=Model, field=field):
|
||||
instances = Model.objects.bulk_create(Model(**{field: initial}) for _ in range(20))
|
||||
for instance in instances:
|
||||
setattr(instance, field, new)
|
||||
Model.objects.bulk_update(instances, [field])
|
||||
self.assertSequenceEqual(Model.objects.filter(**{field: new}), instances)
|
Loading…
Add table
Add a link
Reference in a new issue