mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
[1.8.x] Fixed #23617 -- Added get_pk_value_on_save()
The method is mainly intended for use with UUIDField. For UUIDField we
want to call the field's default even when primary key value is
explicitly set to None to match the behavior of AutoField.
Thanks to Marc Tamlyn and Tim Graham for review.
Backport of 8adc59038c from master
This commit is contained in:
committed by
Tim Graham
parent
9ffe013caa
commit
43b0131fb5
@@ -87,3 +87,20 @@ class TestAsPrimaryKey(TestCase):
|
||||
PrimaryKeyUUIDModel.objects.create()
|
||||
loaded = PrimaryKeyUUIDModel.objects.get()
|
||||
self.assertIsInstance(loaded.pk, uuid.UUID)
|
||||
|
||||
def test_uuid_pk_on_save(self):
|
||||
saved = PrimaryKeyUUIDModel.objects.create(id=None)
|
||||
loaded = PrimaryKeyUUIDModel.objects.get()
|
||||
self.assertIsNotNone(loaded.id, None)
|
||||
self.assertEqual(loaded.id, saved.id)
|
||||
|
||||
def test_uuid_pk_on_bulk_create(self):
|
||||
u1 = PrimaryKeyUUIDModel()
|
||||
u2 = PrimaryKeyUUIDModel(id=None)
|
||||
PrimaryKeyUUIDModel.objects.bulk_create([u1, u2])
|
||||
# Check that the two objects were correctly created.
|
||||
u1_found = PrimaryKeyUUIDModel.objects.filter(id=u1.id).exists()
|
||||
u2_found = PrimaryKeyUUIDModel.objects.exclude(id=u1.id).exists()
|
||||
self.assertTrue(u1_found)
|
||||
self.assertTrue(u2_found)
|
||||
self.assertEqual(PrimaryKeyUUIDModel.objects.count(), 2)
|
||||
|
||||
Reference in New Issue
Block a user