1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #35554, Refs #35060 -- Corrected deprecated *args parsing in Model.save()/asave().

The transitional logic added to deprecate the usage of *args for
Model.save()/asave() introduced two issues that this branch fixes:
 * Passing extra positional arguments no longer raised TypeError.
 * Passing a positional but empty update_fields would save all fields.

Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
This commit is contained in:
Adam Johnson
2024-06-25 17:12:10 +01:00
committed by GitHub
parent bcc327aa32
commit 28522c3c8d
3 changed files with 110 additions and 24 deletions

View File

@@ -210,6 +210,35 @@ class ModelInstanceCreationTests(TestCase):
a.save(False, False, None, None)
self.assertEqual(Article.objects.count(), 1)
def test_save_deprecation_positional_arguments_used(self):
a = Article()
fields = ["headline"]
with (
self.assertWarns(RemovedInDjango60Warning),
mock.patch.object(a, "save_base") as mock_save_base,
):
a.save(None, 1, 2, fields)
self.assertEqual(
mock_save_base.mock_calls,
[
mock.call(
using=2,
force_insert=None,
force_update=1,
update_fields=frozenset(fields),
)
],
)
def test_save_too_many_positional_arguments(self):
a = Article()
msg = "Model.save() takes from 1 to 5 positional arguments but 6 were given"
with (
self.assertWarns(RemovedInDjango60Warning),
self.assertRaisesMessage(TypeError, msg),
):
a.save(False, False, None, None, None)
async def test_asave_deprecation(self):
a = Article(headline="original", pub_date=datetime(2014, 5, 16))
msg = "Passing positional arguments to asave() is deprecated"
@@ -217,6 +246,35 @@ class ModelInstanceCreationTests(TestCase):
await a.asave(False, False, None, None)
self.assertEqual(await Article.objects.acount(), 1)
async def test_asave_deprecation_positional_arguments_used(self):
a = Article()
fields = ["headline"]
with (
self.assertWarns(RemovedInDjango60Warning),
mock.patch.object(a, "save_base") as mock_save_base,
):
await a.asave(None, 1, 2, fields)
self.assertEqual(
mock_save_base.mock_calls,
[
mock.call(
using=2,
force_insert=None,
force_update=1,
update_fields=frozenset(fields),
)
],
)
async def test_asave_too_many_positional_arguments(self):
a = Article()
msg = "Model.asave() takes from 1 to 5 positional arguments but 6 were given"
with (
self.assertWarns(RemovedInDjango60Warning),
self.assertRaisesMessage(TypeError, msg),
):
await a.asave(False, False, None, None, None)
@ignore_warnings(category=RemovedInDjango60Warning)
def test_save_positional_arguments(self):
a = Article.objects.create(headline="original", pub_date=datetime(2014, 5, 16))