Create a project 'what' with an app 'why' (I used 1.8.4 but it was tested
with master branch):
django-admin startproject what
cd what
python manage.py startapp why
Use this files to check the bug:
what/settings.py
{{{
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'why',
)
...
}}}
why/models.py
{{{
from django.db import models
# Create your models here.
class WhyMe(models.Model):
name = models.CharField(max_length=255)
}}}
why/admin.py
{{{
from django.contrib import admin
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from why.models import WhyMe
class WhyMeAdminForm(ModelForm):
def clean_name(self):
name = self.cleaned_data['name']
if name.startswith('xxx'):
raise ValidationError('don\'t use xxx!', code='invalid')
return name
@admin.register(WhyMe)
class WhyMeAdmin(admin.ModelAdmin):
form = WhyMeAdminForm
}}}
why/tests.py
{{{
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
class WhyMeAdminTest(TestCase):
def setUp(self):
self.user = User.objects.create_superuser(username='chuck',
email='ch...@internet.com', password='no')
self.client.login(username='chuck', password='no')
def test_custome_validation(self):
url = reverse('admin:why_whyme_add')
data = {
'name': 'xxxDiegueus9'
}
response = self.client.post(url, data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertFormError(response, 'adminform', 'name', ['don\'t use
xxx!'])
}}}
Finally run the tests and the result would be something like:
{{{
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_custome_validation (why.tests.WhyMeAdminTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/diegueus9/.virtualenvs/django1.8/what/why/tests.py", line
18, in test_custome_validation
self.assertFormError(response, 'adminform', 'name', ['don\'t use
xxx!'])
File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-
packages/Django-1.11.dev20160923192832-py2.7.egg/django/test/testcases.py",
line 428, in assertFormError
if field in context[form].errors:
AttributeError: 'AdminForm' object has no attribute 'errors'
----------------------------------------------------------------------
Ran 1 test in 0.771s
FAILED (errors=1)
Destroying test database for alias 'default'...
}}}
In the attached file I show that the validation works fine in the admin.
I did a little of debug using ipdb and noticed that the errors are in
'AdminForm.form.errors', perhaps it should be added a property to the
AdminForm?
This also fails using django 1.8.x, 1.9.x, 1.10.x
--
Ticket URL: <https://code.djangoproject.com/ticket/27266>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "Screen Shot 2016-09-23 at 17.22.19.png" added.
Custom validation in admin
* needs_better_patch: => 0
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
I'd rather not have those form and formset wrappers in the first place,
but as long as we have them, it seems like a sensible idea. Can you review
my [https://github.com/django/django/pull/7296 PR]?
--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:1>
Comment (by Diego Andrés Sanabria Martín):
Tim, it looks good to me, I just wrote a comment:
" just wonder if self.formset.non_form_errors ==
self.form.non_form_errors."
--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:2>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"6f3c78dbe6f17997aa9d115d041bbb0318061ba7" 6f3c78d]:
{{{
#!CommitTicketReference repository=""
revision="6f3c78dbe6f17997aa9d115d041bbb0318061ba7"
Fixed #27266 -- Allowed using assertFormError()/assertFormsetError() in
admin forms and formsets.
Thanks Diego Andrés Sanabria Martín for the report and review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:3>