mirror of
https://github.com/django/django.git
synced 2025-10-25 22:56:12 +00:00
[1.11.x] Fixed #28222 -- Allowed settable properties in QuerySet.update_or_create()/get_or_create() defaults.
Backport of 37ab3c3f9d from master
This commit is contained in:
@@ -77,6 +77,11 @@ class GetOrCreateTests(TestCase):
|
||||
"""
|
||||
Thing.objects.get_or_create(pk=1)
|
||||
|
||||
def test_get_or_create_with_model_property_defaults(self):
|
||||
"""Using a property with a setter implemented is allowed."""
|
||||
t, _ = Thing.objects.get_or_create(defaults={'capitalized_name_property': 'annie'}, pk=1)
|
||||
self.assertEqual(t.name, 'Annie')
|
||||
|
||||
def test_get_or_create_on_related_manager(self):
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
# Create a book through the publisher.
|
||||
@@ -336,6 +341,11 @@ class UpdateOrCreateTests(TestCase):
|
||||
"""
|
||||
Thing.objects.update_or_create(pk=1)
|
||||
|
||||
def test_update_or_create_with_model_property_defaults(self):
|
||||
"""Using a property with a setter implemented is allowed."""
|
||||
t, _ = Thing.objects.get_or_create(defaults={'capitalized_name_property': 'annie'}, pk=1)
|
||||
self.assertEqual(t.name, 'Annie')
|
||||
|
||||
def test_error_contains_full_traceback(self):
|
||||
"""
|
||||
update_or_create should raise IntegrityErrors with the full traceback.
|
||||
@@ -522,3 +532,11 @@ class InvalidCreateArgumentsTests(SimpleTestCase):
|
||||
def test_multiple_invalid_fields(self):
|
||||
with self.assertRaisesMessage(FieldError, "Invalid field name(s) for model Thing: 'invalid', 'nonexistent'"):
|
||||
Thing.objects.update_or_create(name='a', nonexistent='b', defaults={'invalid': 'c'})
|
||||
|
||||
def test_property_attribute_without_setter_defaults(self):
|
||||
with self.assertRaisesMessage(FieldError, "Invalid field name(s) for model Thing: 'name_in_all_caps'"):
|
||||
Thing.objects.update_or_create(name='a', defaults={'name_in_all_caps': 'FRANK'})
|
||||
|
||||
def test_property_attribute_without_setter_kwargs(self):
|
||||
with self.assertRaisesMessage(FieldError, "Invalid field name(s) for model Thing: 'name_in_all_caps'"):
|
||||
Thing.objects.update_or_create(name_in_all_caps='FRANK', defaults={'name': 'Frank'})
|
||||
|
||||
Reference in New Issue
Block a user