1
from __future__ import absolute_import
3
from django import forms
4
from django.contrib import admin
5
from django.core.exceptions import ImproperlyConfigured
6
from django.test import TestCase
8
from .models import Song, Book, Album, TwoAlbumFKAndAnE, State, City
11
class SongForm(forms.ModelForm):
14
class ValidFields(admin.ModelAdmin):
18
class ValidFormFieldsets(admin.ModelAdmin):
19
def get_form(self, request, obj=None, **kwargs):
20
class ExtraFieldForm(SongForm):
21
name = forms.CharField(max_length=50)
30
class ValidationTestCase(TestCase):
32
def test_readonly_and_editable(self):
33
class SongAdmin(admin.ModelAdmin):
34
readonly_fields = ["original_release"]
37
"fields": ["title", "original_release"],
40
SongAdmin.validate(Song)
42
def test_custom_modelforms_with_fields_fieldsets(self):
44
# Regression test for #8027: custom ModelForms with fields/fieldsets
46
ValidFields.validate(Song)
48
def test_custom_get_form_with_fieldsets(self):
50
Ensure that the fieldsets validation is skipped when the ModelAdmin.get_form() method
54
ValidFormFieldsets.validate(Song)
56
def test_exclude_values(self):
58
Tests for basic validation of 'exclude' option values (#12689)
60
class ExcludedFields1(admin.ModelAdmin):
62
self.assertRaisesMessage(ImproperlyConfigured,
63
"'ExcludedFields1.exclude' must be a list or tuple.",
64
ExcludedFields1.validate,
67
def test_exclude_duplicate_values(self):
68
class ExcludedFields2(admin.ModelAdmin):
69
exclude = ('name', 'name')
70
self.assertRaisesMessage(ImproperlyConfigured,
71
"There are duplicate field(s) in ExcludedFields2.exclude",
72
ExcludedFields2.validate,
75
def test_exclude_in_inline(self):
76
class ExcludedFieldsInline(admin.TabularInline):
80
class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
82
inlines = [ExcludedFieldsInline]
84
self.assertRaisesMessage(ImproperlyConfigured,
85
"'ExcludedFieldsInline.exclude' must be a list or tuple.",
86
ExcludedFieldsAlbumAdmin.validate,
89
def test_exclude_inline_model_admin(self):
91
# Regression test for #9932 - exclude in InlineModelAdmin
92
# should not contain the ForeignKey field used in ModelAdmin.model
94
class SongInline(admin.StackedInline):
98
class AlbumAdmin(admin.ModelAdmin):
100
inlines = [SongInline]
102
self.assertRaisesMessage(ImproperlyConfigured,
103
"SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
107
def test_app_label_in_admin_validation(self):
109
Regression test for #15669 - Include app label in admin validation messages
111
class RawIdNonexistingAdmin(admin.ModelAdmin):
112
raw_id_fields = ('nonexisting',)
114
self.assertRaisesMessage(ImproperlyConfigured,
115
"'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.",
116
RawIdNonexistingAdmin.validate,
119
def test_fk_exclusion(self):
121
Regression test for #11709 - when testing for fk excluding (when exclude is
122
given) make sure fk_name is honored or things blow up when there is more
123
than one fk to the parent model.
125
class TwoAlbumFKAndAnEInline(admin.TabularInline):
126
model = TwoAlbumFKAndAnE
129
class MyAdmin(admin.ModelAdmin):
130
inlines = [TwoAlbumFKAndAnEInline]
131
MyAdmin.validate(Album)
134
def test_inline_self_validation(self):
135
class TwoAlbumFKAndAnEInline(admin.TabularInline):
136
model = TwoAlbumFKAndAnE
137
class MyAdmin(admin.ModelAdmin):
138
inlines = [TwoAlbumFKAndAnEInline]
140
self.assertRaisesMessage(Exception,
141
"<class 'admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'admin_validation.models.Album'>",
142
MyAdmin.validate, Album)
144
def test_inline_with_specified(self):
145
class TwoAlbumFKAndAnEInline(admin.TabularInline):
146
model = TwoAlbumFKAndAnE
149
class MyAdmin(admin.ModelAdmin):
150
inlines = [TwoAlbumFKAndAnEInline]
151
MyAdmin.validate(Album)
153
def test_readonly(self):
154
class SongAdmin(admin.ModelAdmin):
155
readonly_fields = ("title",)
157
SongAdmin.validate(Song)
159
def test_readonly_on_method(self):
160
def my_function(obj):
163
class SongAdmin(admin.ModelAdmin):
164
readonly_fields = (my_function,)
166
SongAdmin.validate(Song)
168
def test_readonly_on_modeladmin(self):
169
class SongAdmin(admin.ModelAdmin):
170
readonly_fields = ("readonly_method_on_modeladmin",)
172
def readonly_method_on_modeladmin(self, obj):
175
SongAdmin.validate(Song)
177
def test_readonly_method_on_model(self):
178
class SongAdmin(admin.ModelAdmin):
179
readonly_fields = ("readonly_method_on_model",)
181
SongAdmin.validate(Song)
183
def test_nonexistant_field(self):
184
class SongAdmin(admin.ModelAdmin):
185
readonly_fields = ("title", "nonexistant")
187
self.assertRaisesMessage(ImproperlyConfigured,
188
"SongAdmin.readonly_fields[1], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.",
192
def test_nonexistant_field_on_inline(self):
193
class CityInline(admin.TabularInline):
195
readonly_fields=['i_dont_exist'] # Missing attribute
197
self.assertRaisesMessage(ImproperlyConfigured,
198
"CityInline.readonly_fields[0], 'i_dont_exist' is not a callable or an attribute of 'CityInline' or found in the model 'City'.",
202
def test_extra(self):
203
class SongAdmin(admin.ModelAdmin):
204
def awesome_song(self, instance):
205
if instance.title == "Born to Run":
207
return "Status unknown."
208
SongAdmin.validate(Song)
210
def test_readonly_lambda(self):
211
class SongAdmin(admin.ModelAdmin):
212
readonly_fields = (lambda obj: "test",)
214
SongAdmin.validate(Song)
216
def test_graceful_m2m_fail(self):
218
Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
219
specifies the 'through' option is included in the 'fields' or the 'fieldsets'
223
class BookAdmin(admin.ModelAdmin):
226
self.assertRaisesMessage(ImproperlyConfigured,
227
"'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
231
def test_cannot_include_through(self):
232
class FieldsetBookAdmin(admin.ModelAdmin):
234
('Header 1', {'fields': ('name',)}),
235
('Header 2', {'fields': ('authors',)}),
237
self.assertRaisesMessage(ImproperlyConfigured,
238
"'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
239
FieldsetBookAdmin.validate,
242
def test_nested_fields(self):
243
class NestedFieldsAdmin(admin.ModelAdmin):
244
fields = ('price', ('name', 'subtitle'))
245
NestedFieldsAdmin.validate(Book)
247
def test_nested_fieldsets(self):
248
class NestedFieldsetAdmin(admin.ModelAdmin):
250
('Main', {'fields': ('price', ('name', 'subtitle'))}),
252
NestedFieldsetAdmin.validate(Book)
254
def test_explicit_through_override(self):
256
Regression test for #12209 -- If the explicitly provided through model
257
is specified as a string, the admin should still be able use
258
Model.m2m_field.through
261
class AuthorsInline(admin.TabularInline):
262
model = Book.authors.through
264
class BookAdmin(admin.ModelAdmin):
265
inlines = [AuthorsInline]
267
# If the through model is still a string (and hasn't been resolved to a model)
268
# the validation will fail.
269
BookAdmin.validate(Book)
271
def test_non_model_fields(self):
273
Regression for ensuring ModelAdmin.fields can contain non-model fields
274
that broke with r11737
276
class SongForm(forms.ModelForm):
277
extra_data = forms.CharField()
279
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
281
fields = ['title', 'extra_data']
283
FieldsOnFormOnlyAdmin.validate(Song)
285
def test_non_model_first_field(self):
287
Regression for ensuring ModelAdmin.field can handle first elem being a
288
non-model field (test fix for UnboundLocalError introduced with r16225).
290
class SongForm(forms.ModelForm):
291
extra_data = forms.CharField()
297
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
299
fields = ['extra_data', 'title']
301
FieldsOnFormOnlyAdmin.validate(Song)