mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #26884 -- Evaluated callables in QuerySet.update_or_create()'s defaults when updating.
This commit is contained in:
parent
b84ecaa736
commit
841cb45847
@ -489,7 +489,7 @@ class QuerySet(object):
|
||||
if created:
|
||||
return obj, created
|
||||
for k, v in six.iteritems(defaults):
|
||||
setattr(obj, k, v)
|
||||
setattr(obj, k, v() if callable(v) else v)
|
||||
obj.save(using=self.db)
|
||||
return obj, False
|
||||
|
||||
|
@ -404,9 +404,21 @@ class UpdateOrCreateTests(TestCase):
|
||||
self.assertFalse(created)
|
||||
self.assertEqual(obj.defaults, 'another testing')
|
||||
|
||||
def test_update_callable_default(self):
|
||||
def test_create_callable_default(self):
|
||||
obj, created = Person.objects.update_or_create(
|
||||
first_name='George', last_name='Harrison',
|
||||
defaults={'birthday': lambda: date(1943, 2, 25)},
|
||||
)
|
||||
self.assertIs(created, True)
|
||||
self.assertEqual(obj.birthday, date(1943, 2, 25))
|
||||
|
||||
def test_update_callable_default(self):
|
||||
Person.objects.update_or_create(
|
||||
first_name='George', last_name='Harrison', birthday=date(1942, 2, 25),
|
||||
)
|
||||
obj, created = Person.objects.update_or_create(
|
||||
first_name='George',
|
||||
defaults={'last_name': lambda: 'NotHarrison'},
|
||||
)
|
||||
self.assertIs(created, False)
|
||||
self.assertEqual(obj.last_name, 'NotHarrison')
|
||||
|
Loading…
Reference in New Issue
Block a user