summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2021-02-27 18:56:44 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-01 20:26:39 +0100
commit73b1b225ce1a3318d4478f90cc0db0a260aba3aa (patch)
tree03a5ccf7187acb6996c07ecaa09e2501a985729a
parent712fe12466cfe4b7e31c61418c85ce36a211261a (diff)
Fixed #22640 -- Raised TypeError when instantiating model with keyword and positional args for the same field.
-rw-r--r--django/db/models/base.py6
-rw-r--r--tests/basic/tests.py9
2 files changed, 14 insertions, 1 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 27c9ff6461..55b1691166 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -442,7 +442,11 @@ class Model(metaclass=ModelBase):
if val is _DEFERRED:
continue
_setattr(self, field.attname, val)
- kwargs.pop(field.name, None)
+ if kwargs.pop(field.name, NOT_PROVIDED) is not NOT_PROVIDED:
+ raise TypeError(
+ f"{cls.__qualname__}() got both positional and "
+ f"keyword arguments for field '{field.name}'."
+ )
# Now we're left with the unprocessed fields that *must* come from
# keywords, or default.
diff --git a/tests/basic/tests.py b/tests/basic/tests.py
index 43c7ccdfa6..c99fc7e723 100644
--- a/tests/basic/tests.py
+++ b/tests/basic/tests.py
@@ -68,6 +68,15 @@ class ModelInstanceCreationTests(TestCase):
a.save()
self.assertEqual(a.headline, 'Fourth article')
+ def test_positional_and_keyword_args_for_the_same_field(self):
+ msg = "Article() got both positional and keyword arguments for field '%s'."
+ with self.assertRaisesMessage(TypeError, msg % 'headline'):
+ Article(None, 'Fifth article', headline='Other headline.')
+ with self.assertRaisesMessage(TypeError, msg % 'headline'):
+ Article(None, 'Sixth article', headline='')
+ with self.assertRaisesMessage(TypeError, msg % 'pub_date'):
+ Article(None, 'Seventh article', datetime(2021, 3, 1), pub_date=None)
+
def test_cannot_create_instance_with_invalid_kwargs(self):
with self.assertRaisesMessage(TypeError, "Article() got an unexpected keyword argument 'foo'"):
Article(