From f2b665f88610b208196b5753b09b4a5cfb322417 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 6 Aug 2015 17:43:55 -0400 Subject: [PATCH] Fixed #25241 -- Corrected ModelForm.save() error message when saving invalid form with UUIDField pk. --- django/forms/models.py | 2 +- tests/model_forms/models.py | 6 ++++++ tests/model_forms/test_uuid.py | 29 +++++++++++++++++++++++++++++ tests/model_forms/tests.py | 2 +- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/model_forms/test_uuid.py diff --git a/django/forms/models.py b/django/forms/models.py index 31e4bbaa66..7592c09787 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -454,7 +454,7 @@ class BaseModelForm(BaseForm): If commit=True, then the changes to ``instance`` will be saved to the database. Returns ``instance``. """ - if self.instance.pk is None: + if self.instance._state.adding: fail_message = 'created' else: fail_message = 'changed' diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index f7dca5bb77..b68714af4c 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -11,6 +11,7 @@ from __future__ import unicode_literals import datetime import os import tempfile +import uuid from django.core import validators from django.core.exceptions import ValidationError @@ -447,3 +448,8 @@ class Photo(models.Model): def save(self, force_insert=False, force_update=False): super(Photo, self).save(force_insert, force_update) self._savecount += 1 + + +class UUIDPK(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=30) diff --git a/tests/model_forms/test_uuid.py b/tests/model_forms/test_uuid.py new file mode 100644 index 0000000000..4dc7d511d7 --- /dev/null +++ b/tests/model_forms/test_uuid.py @@ -0,0 +1,29 @@ +from __future__ import unicode_literals + +from django import forms +from django.test import TestCase + +from .models import UUIDPK + + +class UUIDPKForm(forms.ModelForm): + class Meta: + model = UUIDPK + fields = '__all__' + + +class ModelFormBaseTest(TestCase): + def test_create_save_error(self): + form = UUIDPKForm({}) + self.assertFalse(form.is_valid()) + msg = "The UUIDPK could not be created because the data didn't validate." + with self.assertRaisesMessage(ValueError, msg): + form.save() + + def test_update_save_error(self): + obj = UUIDPK.objects.create(name='foo') + form = UUIDPKForm({}, instance=obj) + self.assertFalse(form.is_valid()) + msg = "The UUIDPK could not be changed because the data didn't validate." + with self.assertRaisesMessage(ValueError, msg): + form.save() diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 7de74e4343..d118785bd4 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -1978,7 +1978,7 @@ class FileAndImageFieldTests(TestCase): form = FPForm() names = [p[1] for p in form['path'].field.choices] names.sort() - self.assertEqual(names, ['---------', '__init__.py', 'models.py', 'tests.py']) + self.assertEqual(names, ['---------', '__init__.py', 'models.py', 'test_uuid.py', 'tests.py']) @skipUnless(test_images, "Pillow not installed") def test_image_field(self):