mirror of
https://github.com/django/django.git
synced 2025-03-13 19:00:45 +00:00
Fixed #29517 -- Rephrased error message when passing incorrect kwarg to model constructor
This commit is contained in:
parent
02cd16a7a0
commit
4c36414323
@ -481,7 +481,7 @@ class Model(metaclass=ModelBase):
|
|||||||
except (AttributeError, FieldDoesNotExist):
|
except (AttributeError, FieldDoesNotExist):
|
||||||
pass
|
pass
|
||||||
for kwarg in kwargs:
|
for kwarg in kwargs:
|
||||||
raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)
|
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, list(kwargs)[0]))
|
||||||
super().__init__()
|
super().__init__()
|
||||||
post_init.send(sender=cls, instance=self)
|
post_init.send(sender=cls, instance=self)
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class ModelInstanceCreationTests(TestCase):
|
|||||||
self.assertEqual(a.headline, 'Fourth article')
|
self.assertEqual(a.headline, 'Fourth article')
|
||||||
|
|
||||||
def test_cannot_create_instance_with_invalid_kwargs(self):
|
def test_cannot_create_instance_with_invalid_kwargs(self):
|
||||||
with self.assertRaisesMessage(TypeError, "'foo' is an invalid keyword argument for this function"):
|
with self.assertRaisesMessage(TypeError, "Article() got an unexpected keyword argument 'foo'"):
|
||||||
Article(
|
Article(
|
||||||
id=None,
|
id=None,
|
||||||
headline='Some headline',
|
headline='Some headline',
|
||||||
|
@ -18,7 +18,7 @@ class PropertyTests(TestCase):
|
|||||||
setattr(self.a, 'full_name', 'Paul McCartney')
|
setattr(self.a, 'full_name', 'Paul McCartney')
|
||||||
|
|
||||||
# And cannot be used to initialize the class.
|
# And cannot be used to initialize the class.
|
||||||
with self.assertRaisesMessage(TypeError, "'full_name' is an invalid keyword argument"):
|
with self.assertRaisesMessage(TypeError, "Person() got an unexpected keyword argument 'full_name'"):
|
||||||
Person(full_name='Paul McCartney')
|
Person(full_name='Paul McCartney')
|
||||||
|
|
||||||
# But "full_name_2" has, and it can be used to initialize the class.
|
# But "full_name_2" has, and it can be used to initialize the class.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user