mirror of
https://github.com/django/django.git
synced 2025-09-26 12:09:19 +00:00
Fixed #12674 -- provide a way to override admin validation
Moved admin validation code to classes and have those be class attributes to the ModelAdmin classes.
This commit is contained in:
parent
886f7cc751
commit
4ad1eb1c14
5 changed files with 525 additions and 540 deletions
|
@ -2,7 +2,6 @@ from __future__ import absolute_import
|
|||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin.validation import validate, validate_inline
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import TestCase
|
||||
|
||||
|
@ -38,13 +37,13 @@ class ValidationTestCase(TestCase):
|
|||
"fields": ["title", "original_release"],
|
||||
}),
|
||||
]
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_custom_modelforms_with_fields_fieldsets(self):
|
||||
"""
|
||||
# Regression test for #8027: custom ModelForms with fields/fieldsets
|
||||
"""
|
||||
validate(ValidFields, Song)
|
||||
ValidFields.validate(Song)
|
||||
|
||||
def test_custom_get_form_with_fieldsets(self):
|
||||
"""
|
||||
|
@ -52,7 +51,7 @@ class ValidationTestCase(TestCase):
|
|||
is overridden.
|
||||
Refs #19445.
|
||||
"""
|
||||
validate(ValidFormFieldsets, Song)
|
||||
ValidFormFieldsets.validate(Song)
|
||||
|
||||
def test_exclude_values(self):
|
||||
"""
|
||||
|
@ -62,16 +61,16 @@ class ValidationTestCase(TestCase):
|
|||
exclude = ('foo')
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"'ExcludedFields1.exclude' must be a list or tuple.",
|
||||
validate,
|
||||
ExcludedFields1, Book)
|
||||
ExcludedFields1.validate,
|
||||
Book)
|
||||
|
||||
def test_exclude_duplicate_values(self):
|
||||
class ExcludedFields2(admin.ModelAdmin):
|
||||
exclude = ('name', 'name')
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"There are duplicate field(s) in ExcludedFields2.exclude",
|
||||
validate,
|
||||
ExcludedFields2, Book)
|
||||
ExcludedFields2.validate,
|
||||
Book)
|
||||
|
||||
def test_exclude_in_inline(self):
|
||||
class ExcludedFieldsInline(admin.TabularInline):
|
||||
|
@ -84,8 +83,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"'ExcludedFieldsInline.exclude' must be a list or tuple.",
|
||||
validate,
|
||||
ExcludedFieldsAlbumAdmin, Album)
|
||||
ExcludedFieldsAlbumAdmin.validate,
|
||||
Album)
|
||||
|
||||
def test_exclude_inline_model_admin(self):
|
||||
"""
|
||||
|
@ -102,8 +101,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
|
||||
validate,
|
||||
AlbumAdmin, Album)
|
||||
AlbumAdmin.validate,
|
||||
Album)
|
||||
|
||||
def test_app_label_in_admin_validation(self):
|
||||
"""
|
||||
|
@ -114,8 +113,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.",
|
||||
validate,
|
||||
RawIdNonexistingAdmin, Album)
|
||||
RawIdNonexistingAdmin.validate,
|
||||
Album)
|
||||
|
||||
def test_fk_exclusion(self):
|
||||
"""
|
||||
|
@ -127,28 +126,35 @@ class ValidationTestCase(TestCase):
|
|||
model = TwoAlbumFKAndAnE
|
||||
exclude = ("e",)
|
||||
fk_name = "album1"
|
||||
validate_inline(TwoAlbumFKAndAnEInline, None, Album)
|
||||
class MyAdmin(admin.ModelAdmin):
|
||||
inlines = [TwoAlbumFKAndAnEInline]
|
||||
MyAdmin.validate(Album)
|
||||
|
||||
|
||||
def test_inline_self_validation(self):
|
||||
class TwoAlbumFKAndAnEInline(admin.TabularInline):
|
||||
model = TwoAlbumFKAndAnE
|
||||
class MyAdmin(admin.ModelAdmin):
|
||||
inlines = [TwoAlbumFKAndAnEInline]
|
||||
|
||||
self.assertRaisesMessage(Exception,
|
||||
"<class 'admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'admin_validation.models.Album'>",
|
||||
validate_inline,
|
||||
TwoAlbumFKAndAnEInline, None, Album)
|
||||
MyAdmin.validate, Album)
|
||||
|
||||
def test_inline_with_specified(self):
|
||||
class TwoAlbumFKAndAnEInline(admin.TabularInline):
|
||||
model = TwoAlbumFKAndAnE
|
||||
fk_name = "album1"
|
||||
validate_inline(TwoAlbumFKAndAnEInline, None, Album)
|
||||
|
||||
class MyAdmin(admin.ModelAdmin):
|
||||
inlines = [TwoAlbumFKAndAnEInline]
|
||||
MyAdmin.validate(Album)
|
||||
|
||||
def test_readonly(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
readonly_fields = ("title",)
|
||||
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_readonly_on_method(self):
|
||||
def my_function(obj):
|
||||
|
@ -157,7 +163,7 @@ class ValidationTestCase(TestCase):
|
|||
class SongAdmin(admin.ModelAdmin):
|
||||
readonly_fields = (my_function,)
|
||||
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_readonly_on_modeladmin(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
|
@ -166,13 +172,13 @@ class ValidationTestCase(TestCase):
|
|||
def readonly_method_on_modeladmin(self, obj):
|
||||
pass
|
||||
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_readonly_method_on_model(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
readonly_fields = ("readonly_method_on_model",)
|
||||
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_nonexistant_field(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
|
@ -180,8 +186,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"SongAdmin.readonly_fields[1], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.",
|
||||
validate,
|
||||
SongAdmin, Song)
|
||||
SongAdmin.validate,
|
||||
Song)
|
||||
|
||||
def test_nonexistant_field_on_inline(self):
|
||||
class CityInline(admin.TabularInline):
|
||||
|
@ -190,8 +196,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"CityInline.readonly_fields[0], 'i_dont_exist' is not a callable or an attribute of 'CityInline' or found in the model 'City'.",
|
||||
validate_inline,
|
||||
CityInline, None, State)
|
||||
CityInline.validate,
|
||||
City)
|
||||
|
||||
def test_extra(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
|
@ -199,13 +205,13 @@ class ValidationTestCase(TestCase):
|
|||
if instance.title == "Born to Run":
|
||||
return "Best Ever!"
|
||||
return "Status unknown."
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_readonly_lambda(self):
|
||||
class SongAdmin(admin.ModelAdmin):
|
||||
readonly_fields = (lambda obj: "test",)
|
||||
|
||||
validate(SongAdmin, Song)
|
||||
SongAdmin.validate(Song)
|
||||
|
||||
def test_graceful_m2m_fail(self):
|
||||
"""
|
||||
|
@ -219,8 +225,8 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
|
||||
validate,
|
||||
BookAdmin, Book)
|
||||
BookAdmin.validate,
|
||||
Book)
|
||||
|
||||
def test_cannot_include_through(self):
|
||||
class FieldsetBookAdmin(admin.ModelAdmin):
|
||||
|
@ -230,20 +236,20 @@ class ValidationTestCase(TestCase):
|
|||
)
|
||||
self.assertRaisesMessage(ImproperlyConfigured,
|
||||
"'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
|
||||
validate,
|
||||
FieldsetBookAdmin, Book)
|
||||
FieldsetBookAdmin.validate,
|
||||
Book)
|
||||
|
||||
def test_nested_fields(self):
|
||||
class NestedFieldsAdmin(admin.ModelAdmin):
|
||||
fields = ('price', ('name', 'subtitle'))
|
||||
validate(NestedFieldsAdmin, Book)
|
||||
NestedFieldsAdmin.validate(Book)
|
||||
|
||||
def test_nested_fieldsets(self):
|
||||
class NestedFieldsetAdmin(admin.ModelAdmin):
|
||||
fieldsets = (
|
||||
('Main', {'fields': ('price', ('name', 'subtitle'))}),
|
||||
)
|
||||
validate(NestedFieldsetAdmin, Book)
|
||||
NestedFieldsetAdmin.validate(Book)
|
||||
|
||||
def test_explicit_through_override(self):
|
||||
"""
|
||||
|
@ -260,7 +266,7 @@ class ValidationTestCase(TestCase):
|
|||
|
||||
# If the through model is still a string (and hasn't been resolved to a model)
|
||||
# the validation will fail.
|
||||
validate(BookAdmin, Book)
|
||||
BookAdmin.validate(Book)
|
||||
|
||||
def test_non_model_fields(self):
|
||||
"""
|
||||
|
@ -274,7 +280,7 @@ class ValidationTestCase(TestCase):
|
|||
form = SongForm
|
||||
fields = ['title', 'extra_data']
|
||||
|
||||
validate(FieldsOnFormOnlyAdmin, Song)
|
||||
FieldsOnFormOnlyAdmin.validate(Song)
|
||||
|
||||
def test_non_model_first_field(self):
|
||||
"""
|
||||
|
@ -292,4 +298,4 @@ class ValidationTestCase(TestCase):
|
|||
form = SongForm
|
||||
fields = ['extra_data', 'title']
|
||||
|
||||
validate(FieldsOnFormOnlyAdmin, Song)
|
||||
FieldsOnFormOnlyAdmin.validate(Song)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue