mirror of https://github.com/django/django.git
Fixed #25241 -- Corrected ModelForm.save() error message when saving invalid form with UUIDField pk.
This commit is contained in:
parent
3e1bb5cfb8
commit
f2b665f886
|
@ -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'
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue