Fixed #25241 -- Corrected ModelForm.save() error message when saving invalid form with UUIDField pk.

This commit is contained in:
Tim Graham 2015-08-06 17:43:55 -04:00
parent 3e1bb5cfb8
commit f2b665f886
4 changed files with 37 additions and 2 deletions

View File

@ -454,7 +454,7 @@ class BaseModelForm(BaseForm):
If commit=True, then the changes to ``instance`` will be saved to the If commit=True, then the changes to ``instance`` will be saved to the
database. Returns ``instance``. database. Returns ``instance``.
""" """
if self.instance.pk is None: if self.instance._state.adding:
fail_message = 'created' fail_message = 'created'
else: else:
fail_message = 'changed' fail_message = 'changed'

View File

@ -11,6 +11,7 @@ from __future__ import unicode_literals
import datetime import datetime
import os import os
import tempfile import tempfile
import uuid
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -447,3 +448,8 @@ class Photo(models.Model):
def save(self, force_insert=False, force_update=False): def save(self, force_insert=False, force_update=False):
super(Photo, self).save(force_insert, force_update) super(Photo, self).save(force_insert, force_update)
self._savecount += 1 self._savecount += 1
class UUIDPK(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=30)

View File

@ -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()

View File

@ -1978,7 +1978,7 @@ class FileAndImageFieldTests(TestCase):
form = FPForm() form = FPForm()
names = [p[1] for p in form['path'].field.choices] names = [p[1] for p in form['path'].field.choices]
names.sort() 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") @skipUnless(test_images, "Pillow not installed")
def test_image_field(self): def test_image_field(self):