1
from __future__ import unicode_literals
5
from django import forms
6
from django.contrib import admin
7
from django.contrib.contenttypes.admin import GenericStackedInline
8
from django.core import checks
9
from django.core.exceptions import ImproperlyConfigured
10
from django.test import TestCase
12
from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State, Influence
15
class SongForm(forms.ModelForm):
19
class ValidFields(admin.ModelAdmin):
24
class ValidFormFieldsets(admin.ModelAdmin):
25
def get_form(self, request, obj=None, **kwargs):
26
class ExtraFieldForm(SongForm):
27
name = forms.CharField(max_length=50)
37
class SystemChecksTestCase(TestCase):
39
def test_checks_are_performed(self):
40
class MyAdmin(admin.ModelAdmin):
42
def check(self, model, **kwargs):
45
admin.site.register(Song, MyAdmin)
47
errors = checks.run_checks()
49
self.assertEqual(errors, expected)
51
admin.site.unregister(Song)
53
def test_readonly_and_editable(self):
54
class SongAdmin(admin.ModelAdmin):
55
readonly_fields = ["original_release"]
56
list_display = ["pk", "original_release"]
57
list_editable = ["original_release"]
60
"fields": ["title", "original_release"],
64
errors = SongAdmin.check(model=Song)
67
("The value of 'list_editable[0]' refers to 'original_release', "
68
"which is not editable through the admin."),
74
self.assertEqual(errors, expected)
76
def test_editable(self):
77
class SongAdmin(admin.ModelAdmin):
78
list_display = ["pk", "title"]
79
list_editable = ["title"]
82
"fields": ["title", "original_release"],
86
errors = SongAdmin.check(model=Song)
87
self.assertEqual(errors, [])
89
def test_custom_modelforms_with_fields_fieldsets(self):
91
# Regression test for #8027: custom ModelForms with fields/fieldsets
94
errors = ValidFields.check(model=Song)
95
self.assertEqual(errors, [])
97
def test_custom_get_form_with_fieldsets(self):
99
Ensure that the fieldsets checks are skipped when the ModelAdmin.get_form() method
104
errors = ValidFormFieldsets.check(model=Song)
105
self.assertEqual(errors, [])
107
def test_exclude_values(self):
109
Tests for basic system checks of 'exclude' option values (#12689)
112
class ExcludedFields1(admin.ModelAdmin):
115
errors = ExcludedFields1.check(model=Book)
118
"The value of 'exclude' must be a list or tuple.",
124
self.assertEqual(errors, expected)
126
def test_exclude_duplicate_values(self):
127
class ExcludedFields2(admin.ModelAdmin):
128
exclude = ('name', 'name')
130
errors = ExcludedFields2.check(model=Book)
133
"The value of 'exclude' contains duplicate field(s).",
139
self.assertEqual(errors, expected)
141
def test_exclude_in_inline(self):
142
class ExcludedFieldsInline(admin.TabularInline):
146
class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
148
inlines = [ExcludedFieldsInline]
150
errors = ExcludedFieldsAlbumAdmin.check(model=Album)
153
"The value of 'exclude' must be a list or tuple.",
155
obj=ExcludedFieldsInline,
159
self.assertEqual(errors, expected)
161
def test_exclude_inline_model_admin(self):
163
Regression test for #9932 - exclude in InlineModelAdmin should not
164
contain the ForeignKey field used in ModelAdmin.model
167
class SongInline(admin.StackedInline):
171
class AlbumAdmin(admin.ModelAdmin):
173
inlines = [SongInline]
175
errors = AlbumAdmin.check(model=Album)
178
("Cannot exclude the field 'album', because it is the foreign key "
179
"to the parent model 'admin_checks.Album'."),
185
self.assertEqual(errors, expected)
187
def test_valid_generic_inline_model_admin(self):
189
Regression test for #22034 - check that generic inlines don't look for
190
normal ForeignKey relations.
193
class InfluenceInline(GenericStackedInline):
196
class SongAdmin(admin.ModelAdmin):
197
inlines = [InfluenceInline]
199
errors = SongAdmin.check(model=Song)
200
self.assertEqual(errors, [])
202
def test_generic_inline_model_admin_non_generic_model(self):
204
Ensure that a model without a GenericForeignKey raises problems if it's included
205
in an GenericInlineModelAdmin definition.
208
class BookInline(GenericStackedInline):
211
class SongAdmin(admin.ModelAdmin):
212
inlines = [BookInline]
214
errors = SongAdmin.check(model=Song)
217
"'admin_checks.Book' has no GenericForeignKey.",
223
self.assertEqual(errors, expected)
225
def test_generic_inline_model_admin_bad_ct_field(self):
226
"A GenericInlineModelAdmin raises problems if the ct_field points to a non-existent field."
228
class InfluenceInline(GenericStackedInline):
230
ct_field = 'nonexistent'
232
class SongAdmin(admin.ModelAdmin):
233
inlines = [InfluenceInline]
235
errors = SongAdmin.check(model=Song)
238
"'ct_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
244
self.assertEqual(errors, expected)
246
def test_generic_inline_model_admin_bad_fk_field(self):
247
"A GenericInlineModelAdmin raises problems if the ct_fk_field points to a non-existent field."
249
class InfluenceInline(GenericStackedInline):
251
ct_fk_field = 'nonexistent'
253
class SongAdmin(admin.ModelAdmin):
254
inlines = [InfluenceInline]
256
errors = SongAdmin.check(model=Song)
259
"'ct_fk_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
265
self.assertEqual(errors, expected)
267
def test_generic_inline_model_admin_non_gfk_ct_field(self):
268
"A GenericInlineModelAdmin raises problems if the ct_field points to a field that isn't part of a GenericForeignKey"
270
class InfluenceInline(GenericStackedInline):
274
class SongAdmin(admin.ModelAdmin):
275
inlines = [InfluenceInline]
277
errors = SongAdmin.check(model=Song)
280
"'admin_checks.Influence' has no GenericForeignKey using content type field 'name' and object ID field 'object_id'.",
286
self.assertEqual(errors, expected)
288
def test_generic_inline_model_admin_non_gfk_fk_field(self):
289
"A GenericInlineModelAdmin raises problems if the ct_fk_field points to a field that isn't part of a GenericForeignKey"
291
class InfluenceInline(GenericStackedInline):
295
class SongAdmin(admin.ModelAdmin):
296
inlines = [InfluenceInline]
298
errors = SongAdmin.check(model=Song)
301
"'admin_checks.Influence' has no GenericForeignKey using content type field 'content_type' and object ID field 'name'.",
307
self.assertEqual(errors, expected)
309
def test_app_label_in_admin_checks(self):
311
Regression test for #15669 - Include app label in admin system check messages
314
class RawIdNonexistingAdmin(admin.ModelAdmin):
315
raw_id_fields = ('nonexisting',)
317
errors = RawIdNonexistingAdmin.check(model=Album)
320
("The value of 'raw_id_fields[0]' refers to 'nonexisting', which is "
321
"not an attribute of 'admin_checks.Album'."),
323
obj=RawIdNonexistingAdmin,
327
self.assertEqual(errors, expected)
329
def test_fk_exclusion(self):
331
Regression test for #11709 - when testing for fk excluding (when exclude is
332
given) make sure fk_name is honored or things blow up when there is more
333
than one fk to the parent model.
336
class TwoAlbumFKAndAnEInline(admin.TabularInline):
337
model = TwoAlbumFKAndAnE
341
class MyAdmin(admin.ModelAdmin):
342
inlines = [TwoAlbumFKAndAnEInline]
344
errors = MyAdmin.check(model=Album)
345
self.assertEqual(errors, [])
347
def test_inline_self_check(self):
348
class TwoAlbumFKAndAnEInline(admin.TabularInline):
349
model = TwoAlbumFKAndAnE
351
class MyAdmin(admin.ModelAdmin):
352
inlines = [TwoAlbumFKAndAnEInline]
354
errors = MyAdmin.check(model=Album)
357
"'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_checks.Album'.",
359
obj=TwoAlbumFKAndAnEInline,
363
self.assertEqual(errors, expected)
365
def test_inline_with_specified(self):
366
class TwoAlbumFKAndAnEInline(admin.TabularInline):
367
model = TwoAlbumFKAndAnE
370
class MyAdmin(admin.ModelAdmin):
371
inlines = [TwoAlbumFKAndAnEInline]
373
errors = MyAdmin.check(model=Album)
374
self.assertEqual(errors, [])
376
def test_readonly(self):
377
class SongAdmin(admin.ModelAdmin):
378
readonly_fields = ("title",)
380
errors = SongAdmin.check(model=Song)
381
self.assertEqual(errors, [])
383
def test_readonly_on_method(self):
384
def my_function(obj):
387
class SongAdmin(admin.ModelAdmin):
388
readonly_fields = (my_function,)
390
errors = SongAdmin.check(model=Song)
391
self.assertEqual(errors, [])
393
def test_readonly_on_modeladmin(self):
394
class SongAdmin(admin.ModelAdmin):
395
readonly_fields = ("readonly_method_on_modeladmin",)
397
def readonly_method_on_modeladmin(self, obj):
400
errors = SongAdmin.check(model=Song)
401
self.assertEqual(errors, [])
403
def test_readonly_method_on_model(self):
404
class SongAdmin(admin.ModelAdmin):
405
readonly_fields = ("readonly_method_on_model",)
407
errors = SongAdmin.check(model=Song)
408
self.assertEqual(errors, [])
410
def test_nonexistant_field(self):
411
class SongAdmin(admin.ModelAdmin):
412
readonly_fields = ("title", "nonexistent")
414
errors = SongAdmin.check(model=Song)
417
("The value of 'readonly_fields[1]' is not a callable, an attribute "
418
"of 'SongAdmin', or an attribute of 'admin_checks.Song'."),
424
self.assertEqual(errors, expected)
426
def test_nonexistant_field_on_inline(self):
427
class CityInline(admin.TabularInline):
429
readonly_fields = ['i_dont_exist'] # Missing attribute
431
errors = CityInline.check(State)
434
("The value of 'readonly_fields[0]' is not a callable, an attribute "
435
"of 'CityInline', or an attribute of 'admin_checks.City'."),
441
self.assertEqual(errors, expected)
443
def test_extra(self):
444
class SongAdmin(admin.ModelAdmin):
445
def awesome_song(self, instance):
446
if instance.title == "Born to Run":
448
return "Status unknown."
450
errors = SongAdmin.check(model=Song)
451
self.assertEqual(errors, [])
453
def test_readonly_lambda(self):
454
class SongAdmin(admin.ModelAdmin):
455
readonly_fields = (lambda obj: "test",)
457
errors = SongAdmin.check(model=Song)
458
self.assertEqual(errors, [])
460
def test_graceful_m2m_fail(self):
462
Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
463
specifies the 'through' option is included in the 'fields' or the 'fieldsets'
467
class BookAdmin(admin.ModelAdmin):
470
errors = BookAdmin.check(model=Book)
473
("The value of 'fields' cannot include the ManyToManyField 'authors', "
474
"because that field manually specifies a relationship model."),
480
self.assertEqual(errors, expected)
482
def test_cannot_include_through(self):
483
class FieldsetBookAdmin(admin.ModelAdmin):
485
('Header 1', {'fields': ('name',)}),
486
('Header 2', {'fields': ('authors',)}),
489
errors = FieldsetBookAdmin.check(model=Book)
492
("The value of 'fieldsets[1][1][\"fields\"]' cannot include the ManyToManyField "
493
"'authors', because that field manually specifies a relationship model."),
495
obj=FieldsetBookAdmin,
499
self.assertEqual(errors, expected)
501
def test_nested_fields(self):
502
class NestedFieldsAdmin(admin.ModelAdmin):
503
fields = ('price', ('name', 'subtitle'))
505
errors = NestedFieldsAdmin.check(model=Book)
506
self.assertEqual(errors, [])
508
def test_nested_fieldsets(self):
509
class NestedFieldsetAdmin(admin.ModelAdmin):
511
('Main', {'fields': ('price', ('name', 'subtitle'))}),
514
errors = NestedFieldsetAdmin.check(model=Book)
515
self.assertEqual(errors, [])
517
def test_explicit_through_override(self):
519
Regression test for #12209 -- If the explicitly provided through model
520
is specified as a string, the admin should still be able use
521
Model.m2m_field.through
524
class AuthorsInline(admin.TabularInline):
525
model = Book.authors.through
527
class BookAdmin(admin.ModelAdmin):
528
inlines = [AuthorsInline]
530
errors = BookAdmin.check(model=Book)
531
self.assertEqual(errors, [])
533
def test_non_model_fields(self):
535
Regression for ensuring ModelAdmin.fields can contain non-model fields
536
that broke with r11737
539
class SongForm(forms.ModelForm):
540
extra_data = forms.CharField()
542
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
544
fields = ['title', 'extra_data']
546
errors = FieldsOnFormOnlyAdmin.check(model=Song)
547
self.assertEqual(errors, [])
549
def test_non_model_first_field(self):
551
Regression for ensuring ModelAdmin.field can handle first elem being a
552
non-model field (test fix for UnboundLocalError introduced with r16225).
555
class SongForm(forms.ModelForm):
556
extra_data = forms.CharField()
562
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
564
fields = ['extra_data', 'title']
566
errors = FieldsOnFormOnlyAdmin.check(model=Song)
567
self.assertEqual(errors, [])
569
def test_validator_compatibility(self):
570
class MyValidator(object):
571
def validate(self, cls, model):
572
raise ImproperlyConfigured("error!")
574
class MyModelAdmin(admin.ModelAdmin):
575
validator_class = MyValidator
577
with warnings.catch_warnings(record=True):
578
warnings.filterwarnings('ignore', module='django.contrib.admin.options')
579
errors = MyModelAdmin.check(model=Song)
588
self.assertEqual(errors, expected)
590
def test_check_sublists_for_duplicates(self):
591
class MyModelAdmin(admin.ModelAdmin):
592
fields = ['state', ['state']]
594
errors = MyModelAdmin.check(model=Song)
597
"The value of 'fields' contains duplicate field(s).",
603
self.assertEqual(errors, expected)
605
def test_check_fieldset_sublists_for_duplicates(self):
606
class MyModelAdmin(admin.ModelAdmin):
609
'fields': ['title', 'album', ('title', 'album')]
613
errors = MyModelAdmin.check(model=Song)
616
"There are duplicate field(s) in 'fieldsets[0][1]'.",
622
self.assertEqual(errors, expected)