2014-01-20 10:45:21 +08:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
2015-09-10 21:05:31 +08:00
|
|
|
from django.contrib.admin import AdminSite
|
2019-03-06 05:22:09 +08:00
|
|
|
from django.contrib.auth.backends import ModelBackend
|
|
|
|
from django.contrib.auth.middleware import AuthenticationMiddleware
|
2014-03-08 11:24:13 +08:00
|
|
|
from django.contrib.contenttypes.admin import GenericStackedInline
|
2019-03-06 05:22:09 +08:00
|
|
|
from django.contrib.messages.middleware import MessageMiddleware
|
2019-04-04 18:02:47 +08:00
|
|
|
from django.contrib.sessions.middleware import SessionMiddleware
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2017-01-03 05:42:27 +08:00
|
|
|
from .models import (
|
|
|
|
Album, Author, Book, City, Influence, Song, State, TwoAlbumFKAndAnE,
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SongForm(forms.ModelForm):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ValidFields(admin.ModelAdmin):
|
|
|
|
form = SongForm
|
|
|
|
fields = ['title']
|
|
|
|
|
|
|
|
|
|
|
|
class ValidFormFieldsets(admin.ModelAdmin):
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
class ExtraFieldForm(SongForm):
|
|
|
|
name = forms.CharField(max_length=50)
|
|
|
|
return ExtraFieldForm
|
|
|
|
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'fields': ('name',),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-12-14 11:22:37 +08:00
|
|
|
class MyAdmin(admin.ModelAdmin):
|
2015-09-10 21:05:31 +08:00
|
|
|
def check(self, **kwargs):
|
2014-12-14 11:22:37 +08:00
|
|
|
return ['error!']
|
|
|
|
|
|
|
|
|
2019-03-06 05:22:09 +08:00
|
|
|
class AuthenticationMiddlewareSubclass(AuthenticationMiddleware):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MessageMiddlewareSubclass(MessageMiddleware):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ModelBackendSubclass(ModelBackend):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-04-04 18:02:47 +08:00
|
|
|
class SessionMiddlewareSubclass(SessionMiddleware):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-09-09 01:38:07 +08:00
|
|
|
@override_settings(
|
|
|
|
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
|
2018-08-19 23:04:57 +08:00
|
|
|
INSTALLED_APPS=[
|
|
|
|
'django.contrib.admin',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
2018-08-21 05:57:46 +08:00
|
|
|
'django.contrib.messages',
|
2018-08-19 23:04:57 +08:00
|
|
|
'admin_checks',
|
|
|
|
],
|
2014-09-09 01:38:07 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class SystemChecksTestCase(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_checks_are_performed(self):
|
|
|
|
admin.site.register(Song, MyAdmin)
|
|
|
|
try:
|
|
|
|
errors = checks.run_checks()
|
|
|
|
expected = ['error!']
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
finally:
|
|
|
|
admin.site.unregister(Song)
|
2014-12-14 11:22:37 +08:00
|
|
|
|
2015-12-10 20:45:21 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['django.contrib.admin'])
|
2018-08-21 05:57:46 +08:00
|
|
|
def test_apps_dependencies(self):
|
2015-12-10 20:45:21 +08:00
|
|
|
errors = admin.checks.check_dependencies()
|
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.contenttypes' must be in "
|
|
|
|
"INSTALLED_APPS in order to use the admin application.",
|
|
|
|
id="admin.E401",
|
2018-08-21 05:57:46 +08:00
|
|
|
),
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.auth' must be in INSTALLED_APPS in order "
|
|
|
|
"to use the admin application.",
|
|
|
|
id='admin.E405',
|
|
|
|
),
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.messages' must be in INSTALLED_APPS in order "
|
|
|
|
"to use the admin application.",
|
|
|
|
id='admin.E406',
|
|
|
|
),
|
2015-12-10 20:45:21 +08:00
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2017-06-03 06:44:34 +08:00
|
|
|
@override_settings(TEMPLATES=[])
|
|
|
|
def test_no_template_engines(self):
|
2018-08-21 05:57:46 +08:00
|
|
|
self.assertEqual(admin.checks.check_dependencies(), [
|
|
|
|
checks.Error(
|
|
|
|
"A 'django.template.backends.django.DjangoTemplates' "
|
|
|
|
"instance must be configured in TEMPLATES in order to use "
|
|
|
|
"the admin application.",
|
|
|
|
id='admin.E403',
|
|
|
|
)
|
|
|
|
])
|
2017-06-03 06:44:34 +08:00
|
|
|
|
2015-12-10 20:45:21 +08:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [],
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': [],
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
)
|
2018-08-21 05:57:46 +08:00
|
|
|
def test_context_processor_dependencies(self):
|
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.auth.context_processors.auth' must be "
|
|
|
|
"enabled in DjangoTemplates (TEMPLATES) if using the default "
|
|
|
|
"auth backend in order to use the admin application.",
|
|
|
|
id='admin.E402',
|
|
|
|
),
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.messages.context_processors.messages' must "
|
|
|
|
"be enabled in DjangoTemplates (TEMPLATES) in order to use "
|
|
|
|
"the admin application.",
|
|
|
|
id='admin.E404',
|
2020-05-14 16:27:35 +08:00
|
|
|
),
|
|
|
|
checks.Warning(
|
|
|
|
"'django.template.context_processors.request' must be enabled "
|
|
|
|
"in DjangoTemplates (TEMPLATES) in order to use the admin "
|
|
|
|
"navigation sidebar.",
|
|
|
|
id='admin.W411',
|
2018-08-21 05:57:46 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), expected)
|
|
|
|
# The first error doesn't happen if
|
|
|
|
# 'django.contrib.auth.backends.ModelBackend' isn't in
|
|
|
|
# AUTHENTICATION_BACKENDS.
|
|
|
|
with self.settings(AUTHENTICATION_BACKENDS=[]):
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), expected[1:])
|
|
|
|
|
2019-03-06 05:22:09 +08:00
|
|
|
@override_settings(
|
|
|
|
AUTHENTICATION_BACKENDS=['admin_checks.tests.ModelBackendSubclass'],
|
|
|
|
TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [],
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
2020-05-14 16:27:35 +08:00
|
|
|
'context_processors': [
|
|
|
|
'django.template.context_processors.request',
|
|
|
|
'django.contrib.messages.context_processors.messages',
|
|
|
|
],
|
2019-03-06 05:22:09 +08:00
|
|
|
},
|
|
|
|
}],
|
|
|
|
)
|
|
|
|
def test_context_processor_dependencies_model_backend_subclass(self):
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), [
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.auth.context_processors.auth' must be "
|
|
|
|
"enabled in DjangoTemplates (TEMPLATES) if using the default "
|
|
|
|
"auth backend in order to use the admin application.",
|
|
|
|
id='admin.E402',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
2018-08-21 05:57:46 +08:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[
|
|
|
|
{
|
2018-10-04 22:05:22 +08:00
|
|
|
'BACKEND': 'django.template.backends.dummy.TemplateStrings',
|
2018-08-21 05:57:46 +08:00
|
|
|
'DIRS': [],
|
|
|
|
'APP_DIRS': True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [],
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': [
|
2020-05-14 16:27:35 +08:00
|
|
|
'django.template.context_processors.request',
|
2018-08-21 05:57:46 +08:00
|
|
|
'django.contrib.auth.context_processors.auth',
|
|
|
|
'django.contrib.messages.context_processors.messages',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_several_templates_backends(self):
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), [])
|
|
|
|
|
|
|
|
@override_settings(MIDDLEWARE=[])
|
|
|
|
def test_middleware_dependencies(self):
|
2015-12-10 20:45:21 +08:00
|
|
|
errors = admin.checks.check_dependencies()
|
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2018-08-21 05:57:46 +08:00
|
|
|
"'django.contrib.auth.middleware.AuthenticationMiddleware' "
|
|
|
|
"must be in MIDDLEWARE in order to use the admin application.",
|
|
|
|
id='admin.E408',
|
|
|
|
),
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.messages.middleware.MessageMiddleware' "
|
|
|
|
"must be in MIDDLEWARE in order to use the admin application.",
|
|
|
|
id='admin.E409',
|
2019-04-04 18:02:47 +08:00
|
|
|
),
|
|
|
|
checks.Error(
|
|
|
|
"'django.contrib.sessions.middleware.SessionMiddleware' "
|
|
|
|
"must be in MIDDLEWARE in order to use the admin application.",
|
2020-05-25 22:41:24 +08:00
|
|
|
hint=(
|
|
|
|
"Insert "
|
|
|
|
"'django.contrib.sessions.middleware.SessionMiddleware' "
|
|
|
|
"before "
|
|
|
|
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
|
|
|
|
),
|
2019-04-04 18:02:47 +08:00
|
|
|
id='admin.E410',
|
|
|
|
),
|
2015-12-10 20:45:21 +08:00
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2019-03-06 05:22:09 +08:00
|
|
|
@override_settings(MIDDLEWARE=[
|
|
|
|
'admin_checks.tests.AuthenticationMiddlewareSubclass',
|
|
|
|
'admin_checks.tests.MessageMiddlewareSubclass',
|
2019-04-04 18:02:47 +08:00
|
|
|
'admin_checks.tests.SessionMiddlewareSubclass',
|
2019-03-06 05:22:09 +08:00
|
|
|
])
|
|
|
|
def test_middleware_subclasses(self):
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), [])
|
|
|
|
|
|
|
|
@override_settings(MIDDLEWARE=[
|
|
|
|
'django.contrib.does.not.Exist',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
2019-04-04 18:02:47 +08:00
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
2019-03-06 05:22:09 +08:00
|
|
|
])
|
|
|
|
def test_admin_check_ignores_import_error_in_middleware(self):
|
|
|
|
self.assertEqual(admin.checks.check_dependencies(), [])
|
|
|
|
|
2014-12-14 11:22:37 +08:00
|
|
|
def test_custom_adminsite(self):
|
|
|
|
class CustomAdminSite(admin.AdminSite):
|
|
|
|
pass
|
|
|
|
|
|
|
|
custom_site = CustomAdminSite()
|
|
|
|
custom_site.register(Song, MyAdmin)
|
|
|
|
try:
|
|
|
|
errors = checks.run_checks()
|
|
|
|
expected = ['error!']
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
finally:
|
|
|
|
custom_site.unregister(Song)
|
2017-01-03 05:42:27 +08:00
|
|
|
|
|
|
|
def test_allows_checks_relying_on_other_modeladmins(self):
|
|
|
|
class MyBookAdmin(admin.ModelAdmin):
|
|
|
|
def check(self, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
errors = super().check(**kwargs)
|
2017-01-03 05:42:27 +08:00
|
|
|
author_admin = self.admin_site._registry.get(Author)
|
|
|
|
if author_admin is None:
|
|
|
|
errors.append('AuthorAdmin missing!')
|
|
|
|
return errors
|
|
|
|
|
|
|
|
class MyAuthorAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
admin.site.register(Book, MyBookAdmin)
|
|
|
|
admin.site.register(Author, MyAuthorAdmin)
|
|
|
|
try:
|
|
|
|
self.assertEqual(admin.site.check(None), [])
|
|
|
|
finally:
|
|
|
|
admin.site.unregister(Book)
|
|
|
|
admin.site.unregister(Author)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-08-13 16:03:20 +08:00
|
|
|
def test_field_name_not_in_list_display(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
list_editable = ["original_release"]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2015-08-13 16:03:20 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'list_editable[0]' refers to 'original_release', "
|
|
|
|
"which is not contained in 'list_display'.",
|
|
|
|
obj=SongAdmin,
|
|
|
|
id='admin.E122',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2017-06-03 06:44:34 +08:00
|
|
|
def test_list_editable_not_a_list_or_tuple(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
list_editable = 'test'
|
|
|
|
|
|
|
|
self.assertEqual(SongAdmin(Song, AdminSite()).check(), [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'list_editable' must be a list or tuple.",
|
|
|
|
obj=SongAdmin,
|
|
|
|
id='admin.E120',
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_list_editable_missing_field(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
list_editable = ('test',)
|
|
|
|
|
|
|
|
self.assertEqual(SongAdmin(Song, AdminSite()).check(), [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'list_editable[0]' refers to 'test', which is "
|
|
|
|
"not an attribute of 'admin_checks.Song'.",
|
|
|
|
obj=SongAdmin,
|
|
|
|
id='admin.E121',
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_readonly_and_editable(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = ["original_release"]
|
2014-01-24 21:35:17 +08:00
|
|
|
list_display = ["pk", "original_release"]
|
|
|
|
list_editable = ["original_release"]
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": ["title", "original_release"],
|
|
|
|
}),
|
|
|
|
]
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-24 21:35:17 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"The value of 'list_editable[0]' refers to 'original_release', "
|
|
|
|
"which is not editable through the admin.",
|
2014-01-24 21:35:17 +08:00
|
|
|
obj=SongAdmin,
|
2014-03-03 13:27:17 +08:00
|
|
|
id='admin.E125',
|
2014-01-24 21:35:17 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_editable(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["pk", "title"]
|
|
|
|
list_editable = ["title"]
|
2014-01-20 10:45:21 +08:00
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": ["title", "original_release"],
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_custom_modelforms_with_fields_fieldsets(self):
|
|
|
|
"""
|
|
|
|
# Regression test for #8027: custom ModelForms with fields/fieldsets
|
|
|
|
"""
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = ValidFields(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_custom_get_form_with_fieldsets(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The fieldsets checks are skipped when the ModelAdmin.get_form() method
|
2014-01-20 10:45:21 +08:00
|
|
|
is overridden.
|
|
|
|
"""
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = ValidFormFieldsets(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2015-01-11 00:45:05 +08:00
|
|
|
def test_fieldsets_fields_non_tuple(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The first fieldset's fields must be a list/tuple.
|
2015-01-11 00:45:05 +08:00
|
|
|
"""
|
|
|
|
class NotATupleAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["pk", "title"]
|
|
|
|
list_editable = ["title"]
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": "title" # not a tuple
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = NotATupleAdmin(Song, AdminSite()).check()
|
2015-01-11 00:45:05 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2015-08-02 05:32:51 +08:00
|
|
|
"The value of 'fieldsets[0][1]['fields']' must be a list or tuple.",
|
2015-01-11 00:45:05 +08:00
|
|
|
obj=NotATupleAdmin,
|
|
|
|
id='admin.E008',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_nonfirst_fieldset(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The second fieldset's fields must be a list/tuple.
|
2015-01-11 00:45:05 +08:00
|
|
|
"""
|
|
|
|
class NotATupleAdmin(admin.ModelAdmin):
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": ("title",)
|
|
|
|
}),
|
|
|
|
('foo', {
|
|
|
|
"fields": "author" # not a tuple
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = NotATupleAdmin(Song, AdminSite()).check()
|
2015-01-11 00:45:05 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2015-08-02 05:32:51 +08:00
|
|
|
"The value of 'fieldsets[1][1]['fields']' must be a list or tuple.",
|
2015-01-11 00:45:05 +08:00
|
|
|
obj=NotATupleAdmin,
|
|
|
|
id='admin.E008',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_exclude_values(self):
|
|
|
|
"""
|
|
|
|
Tests for basic system checks of 'exclude' option values (#12689)
|
|
|
|
"""
|
|
|
|
class ExcludedFields1(admin.ModelAdmin):
|
|
|
|
exclude = 'foo'
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = ExcludedFields1(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'exclude' must be a list or tuple.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=ExcludedFields1,
|
|
|
|
id='admin.E014',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_exclude_duplicate_values(self):
|
|
|
|
class ExcludedFields2(admin.ModelAdmin):
|
|
|
|
exclude = ('name', 'name')
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = ExcludedFields2(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'exclude' contains duplicate field(s).",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=ExcludedFields2,
|
|
|
|
id='admin.E015',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_exclude_in_inline(self):
|
|
|
|
class ExcludedFieldsInline(admin.TabularInline):
|
|
|
|
model = Song
|
|
|
|
exclude = 'foo'
|
|
|
|
|
|
|
|
class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
|
|
|
|
model = Album
|
|
|
|
inlines = [ExcludedFieldsInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = ExcludedFieldsAlbumAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'exclude' must be a list or tuple.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=ExcludedFieldsInline,
|
|
|
|
id='admin.E014',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_exclude_inline_model_admin(self):
|
|
|
|
"""
|
|
|
|
Regression test for #9932 - exclude in InlineModelAdmin should not
|
|
|
|
contain the ForeignKey field used in ModelAdmin.model
|
|
|
|
"""
|
|
|
|
class SongInline(admin.StackedInline):
|
|
|
|
model = Song
|
|
|
|
exclude = ['album']
|
|
|
|
|
|
|
|
class AlbumAdmin(admin.ModelAdmin):
|
|
|
|
model = Album
|
|
|
|
inlines = [SongInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = AlbumAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"Cannot exclude the field 'album', because it is the foreign key "
|
|
|
|
"to the parent model 'admin_checks.Album'.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=SongInline,
|
|
|
|
id='admin.E201',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2014-03-08 11:24:13 +08:00
|
|
|
def test_valid_generic_inline_model_admin(self):
|
|
|
|
"""
|
|
|
|
Regression test for #22034 - check that generic inlines don't look for
|
|
|
|
normal ForeignKey relations.
|
|
|
|
"""
|
|
|
|
class InfluenceInline(GenericStackedInline):
|
|
|
|
model = Influence
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [InfluenceInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_generic_inline_model_admin_non_generic_model(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A model without a GenericForeignKey raises problems if it's included
|
2017-11-07 11:11:39 +08:00
|
|
|
in a GenericInlineModelAdmin definition.
|
2014-03-08 11:24:13 +08:00
|
|
|
"""
|
|
|
|
class BookInline(GenericStackedInline):
|
|
|
|
model = Book
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [BookInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"'admin_checks.Book' has no GenericForeignKey.",
|
|
|
|
obj=BookInline,
|
|
|
|
id='admin.E301',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_generic_inline_model_admin_bad_ct_field(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
"""
|
|
|
|
A GenericInlineModelAdmin errors if the ct_field points to a
|
|
|
|
nonexistent field.
|
|
|
|
"""
|
2014-03-08 11:24:13 +08:00
|
|
|
class InfluenceInline(GenericStackedInline):
|
|
|
|
model = Influence
|
|
|
|
ct_field = 'nonexistent'
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [InfluenceInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"'ct_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
|
|
|
|
obj=InfluenceInline,
|
|
|
|
id='admin.E302',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_generic_inline_model_admin_bad_fk_field(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
"""
|
|
|
|
A GenericInlineModelAdmin errors if the ct_fk_field points to a
|
|
|
|
nonexistent field.
|
|
|
|
"""
|
2014-03-08 11:24:13 +08:00
|
|
|
class InfluenceInline(GenericStackedInline):
|
|
|
|
model = Influence
|
|
|
|
ct_fk_field = 'nonexistent'
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [InfluenceInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
|
|
|
"'ct_fk_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
|
|
|
|
obj=InfluenceInline,
|
|
|
|
id='admin.E303',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_generic_inline_model_admin_non_gfk_ct_field(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
"""
|
|
|
|
A GenericInlineModelAdmin raises problems if the ct_field points to a
|
|
|
|
field that isn't part of a GenericForeignKey.
|
|
|
|
"""
|
2014-03-08 11:24:13 +08:00
|
|
|
class InfluenceInline(GenericStackedInline):
|
|
|
|
model = Influence
|
|
|
|
ct_field = 'name'
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [InfluenceInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2015-09-12 07:33:12 +08:00
|
|
|
"'admin_checks.Influence' has no GenericForeignKey using "
|
|
|
|
"content type field 'name' and object ID field 'object_id'.",
|
2014-03-08 11:24:13 +08:00
|
|
|
obj=InfluenceInline,
|
|
|
|
id='admin.E304',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_generic_inline_model_admin_non_gfk_fk_field(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
"""
|
|
|
|
A GenericInlineModelAdmin raises problems if the ct_fk_field points to
|
|
|
|
a field that isn't part of a GenericForeignKey.
|
|
|
|
"""
|
2014-03-08 11:24:13 +08:00
|
|
|
class InfluenceInline(GenericStackedInline):
|
|
|
|
model = Influence
|
|
|
|
ct_fk_field = 'name'
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [InfluenceInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-03-08 11:24:13 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2015-09-12 07:33:12 +08:00
|
|
|
"'admin_checks.Influence' has no GenericForeignKey using "
|
|
|
|
"content type field 'content_type' and object ID field 'name'.",
|
2014-03-08 11:24:13 +08:00
|
|
|
obj=InfluenceInline,
|
|
|
|
id='admin.E304',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_app_label_in_admin_checks(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
class RawIdNonexistentAdmin(admin.ModelAdmin):
|
|
|
|
raw_id_fields = ('nonexistent',)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2017-02-03 09:43:21 +08:00
|
|
|
errors = RawIdNonexistentAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2017-02-03 09:43:21 +08:00
|
|
|
"The value of 'raw_id_fields[0]' refers to 'nonexistent', "
|
2015-09-12 07:33:12 +08:00
|
|
|
"which is not an attribute of 'admin_checks.Album'.",
|
2017-02-03 09:43:21 +08:00
|
|
|
obj=RawIdNonexistentAdmin,
|
2014-01-20 10:45:21 +08:00
|
|
|
id='admin.E002',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_fk_exclusion(self):
|
|
|
|
"""
|
|
|
|
Regression test for #11709 - when testing for fk excluding (when exclude is
|
|
|
|
given) make sure fk_name is honored or things blow up when there is more
|
|
|
|
than one fk to the parent model.
|
|
|
|
"""
|
|
|
|
class TwoAlbumFKAndAnEInline(admin.TabularInline):
|
|
|
|
model = TwoAlbumFKAndAnE
|
|
|
|
exclude = ("e",)
|
|
|
|
fk_name = "album1"
|
|
|
|
|
|
|
|
class MyAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [TwoAlbumFKAndAnEInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = MyAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_inline_self_check(self):
|
|
|
|
class TwoAlbumFKAndAnEInline(admin.TabularInline):
|
|
|
|
model = TwoAlbumFKAndAnE
|
|
|
|
|
|
|
|
class MyAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [TwoAlbumFKAndAnEInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = MyAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2019-12-17 05:16:39 +08:00
|
|
|
"'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey "
|
|
|
|
"to 'admin_checks.Album'. You must specify a 'fk_name' "
|
|
|
|
"attribute.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=TwoAlbumFKAndAnEInline,
|
|
|
|
id='admin.E202',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_inline_with_specified(self):
|
|
|
|
class TwoAlbumFKAndAnEInline(admin.TabularInline):
|
|
|
|
model = TwoAlbumFKAndAnE
|
|
|
|
fk_name = "album1"
|
|
|
|
|
|
|
|
class MyAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [TwoAlbumFKAndAnEInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = MyAdmin(Album, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2019-12-14 05:00:54 +08:00
|
|
|
def test_inlines_property(self):
|
|
|
|
class CitiesInline(admin.TabularInline):
|
|
|
|
model = City
|
|
|
|
|
|
|
|
class StateAdmin(admin.ModelAdmin):
|
|
|
|
@property
|
|
|
|
def inlines(self):
|
|
|
|
return [CitiesInline]
|
|
|
|
|
|
|
|
errors = StateAdmin(State, AdminSite()).check()
|
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_readonly(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = ("title",)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_readonly_on_method(self):
|
2021-01-14 00:19:22 +08:00
|
|
|
@admin.display
|
2014-01-20 10:45:21 +08:00
|
|
|
def my_function(obj):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = (my_function,)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_readonly_on_modeladmin(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = ("readonly_method_on_modeladmin",)
|
|
|
|
|
2021-01-14 00:19:22 +08:00
|
|
|
@admin.display
|
2014-01-20 10:45:21 +08:00
|
|
|
def readonly_method_on_modeladmin(self, obj):
|
|
|
|
pass
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_readonly_dynamic_attribute_on_modeladmin(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = ("dynamic_method",)
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
if item == "dynamic_method":
|
2021-01-14 00:19:22 +08:00
|
|
|
@admin.display
|
2015-09-10 21:05:31 +08:00
|
|
|
def method(obj):
|
|
|
|
pass
|
|
|
|
return method
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_readonly_method_on_model(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = ("readonly_method_on_model",)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2014-11-04 06:48:03 +08:00
|
|
|
def test_nonexistent_field(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
class SongAdmin(admin.ModelAdmin):
|
2014-04-27 01:18:45 +08:00
|
|
|
readonly_fields = ("title", "nonexistent")
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"The value of 'readonly_fields[1]' is not a callable, an attribute "
|
|
|
|
"of 'SongAdmin', or an attribute of 'admin_checks.Song'.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=SongAdmin,
|
|
|
|
id='admin.E035',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2014-11-04 06:48:03 +08:00
|
|
|
def test_nonexistent_field_on_inline(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
class CityInline(admin.TabularInline):
|
|
|
|
model = City
|
|
|
|
readonly_fields = ['i_dont_exist'] # Missing attribute
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = CityInline(State, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"The value of 'readonly_fields[0]' is not a callable, an attribute "
|
|
|
|
"of 'CityInline', or an attribute of 'admin_checks.City'.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=CityInline,
|
|
|
|
id='admin.E035',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2017-06-03 06:44:34 +08:00
|
|
|
def test_readonly_fields_not_list_or_tuple(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = 'test'
|
|
|
|
|
|
|
|
self.assertEqual(SongAdmin(Song, AdminSite()).check(), [
|
|
|
|
checks.Error(
|
|
|
|
"The value of 'readonly_fields' must be a list or tuple.",
|
|
|
|
obj=SongAdmin,
|
|
|
|
id='admin.E034',
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_extra(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
2021-01-14 00:19:22 +08:00
|
|
|
@admin.display
|
2014-01-20 10:45:21 +08:00
|
|
|
def awesome_song(self, instance):
|
|
|
|
if instance.title == "Born to Run":
|
|
|
|
return "Best Ever!"
|
|
|
|
return "Status unknown."
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_readonly_lambda(self):
|
|
|
|
class SongAdmin(admin.ModelAdmin):
|
|
|
|
readonly_fields = (lambda obj: "test",)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = SongAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_graceful_m2m_fail(self):
|
|
|
|
"""
|
|
|
|
Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
|
|
|
|
specifies the 'through' option is included in the 'fields' or the 'fieldsets'
|
|
|
|
ModelAdmin options.
|
|
|
|
"""
|
|
|
|
class BookAdmin(admin.ModelAdmin):
|
|
|
|
fields = ['authors']
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = BookAdmin(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-08-19 00:45:27 +08:00
|
|
|
"The value of 'fields' cannot include the ManyToManyField 'authors', "
|
2016-02-13 00:36:46 +08:00
|
|
|
"because that field manually specifies a relationship model.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=BookAdmin,
|
|
|
|
id='admin.E013',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_cannot_include_through(self):
|
|
|
|
class FieldsetBookAdmin(admin.ModelAdmin):
|
|
|
|
fieldsets = (
|
|
|
|
('Header 1', {'fields': ('name',)}),
|
|
|
|
('Header 2', {'fields': ('authors',)}),
|
|
|
|
)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = FieldsetBookAdmin(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2016-08-19 00:45:27 +08:00
|
|
|
"The value of 'fieldsets[1][1][\"fields\"]' cannot include the ManyToManyField "
|
|
|
|
"'authors', because that field manually specifies a relationship model.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=FieldsetBookAdmin,
|
|
|
|
id='admin.E013',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_nested_fields(self):
|
|
|
|
class NestedFieldsAdmin(admin.ModelAdmin):
|
|
|
|
fields = ('price', ('name', 'subtitle'))
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = NestedFieldsAdmin(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_nested_fieldsets(self):
|
|
|
|
class NestedFieldsetAdmin(admin.ModelAdmin):
|
|
|
|
fieldsets = (
|
|
|
|
('Main', {'fields': ('price', ('name', 'subtitle'))}),
|
|
|
|
)
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = NestedFieldsetAdmin(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_explicit_through_override(self):
|
|
|
|
"""
|
|
|
|
Regression test for #12209 -- If the explicitly provided through model
|
|
|
|
is specified as a string, the admin should still be able use
|
|
|
|
Model.m2m_field.through
|
|
|
|
"""
|
|
|
|
class AuthorsInline(admin.TabularInline):
|
|
|
|
model = Book.authors.through
|
|
|
|
|
|
|
|
class BookAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [AuthorsInline]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = BookAdmin(Book, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_non_model_fields(self):
|
|
|
|
"""
|
|
|
|
Regression for ensuring ModelAdmin.fields can contain non-model fields
|
|
|
|
that broke with r11737
|
|
|
|
"""
|
|
|
|
class SongForm(forms.ModelForm):
|
|
|
|
extra_data = forms.CharField()
|
|
|
|
|
|
|
|
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
|
|
|
|
form = SongForm
|
|
|
|
fields = ['title', 'extra_data']
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_non_model_first_field(self):
|
|
|
|
"""
|
|
|
|
Regression for ensuring ModelAdmin.field can handle first elem being a
|
|
|
|
non-model field (test fix for UnboundLocalError introduced with r16225).
|
|
|
|
"""
|
|
|
|
class SongForm(forms.ModelForm):
|
|
|
|
extra_data = forms.CharField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Song
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
|
|
|
|
form = SongForm
|
|
|
|
fields = ['extra_data', 'title']
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
|
2014-01-20 10:45:21 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2014-02-15 18:28:09 +08:00
|
|
|
def test_check_sublists_for_duplicates(self):
|
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
|
fields = ['state', ['state']]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = MyModelAdmin(Song, AdminSite()).check()
|
2014-02-15 18:28:09 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"The value of 'fields' contains duplicate field(s).",
|
2014-02-15 18:28:09 +08:00
|
|
|
obj=MyModelAdmin,
|
|
|
|
id='admin.E006'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_check_fieldset_sublists_for_duplicates(self):
|
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
'fields': ['title', 'album', ('title', 'album')]
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = MyModelAdmin(Song, AdminSite()).check()
|
2014-02-15 18:28:09 +08:00
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 13:27:17 +08:00
|
|
|
"There are duplicate field(s) in 'fieldsets[0][1]'.",
|
2014-02-15 18:28:09 +08:00
|
|
|
obj=MyModelAdmin,
|
|
|
|
id='admin.E012'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
2015-02-11 01:15:54 +08:00
|
|
|
|
|
|
|
def test_list_filter_works_on_through_field_even_when_apps_not_ready(self):
|
|
|
|
"""
|
|
|
|
Ensure list_filter can access reverse fields even when the app registry
|
|
|
|
is not ready; refs #24146.
|
|
|
|
"""
|
|
|
|
class BookAdminWithListFilter(admin.ModelAdmin):
|
|
|
|
list_filter = ['authorsbooks__featured']
|
|
|
|
|
|
|
|
# Temporarily pretending apps are not ready yet. This issue can happen
|
|
|
|
# if the value of 'list_filter' refers to a 'through__field'.
|
|
|
|
Book._meta.apps.ready = False
|
|
|
|
try:
|
2015-09-10 21:05:31 +08:00
|
|
|
errors = BookAdminWithListFilter(Book, AdminSite()).check()
|
2015-02-11 01:15:54 +08:00
|
|
|
self.assertEqual(errors, [])
|
|
|
|
finally:
|
|
|
|
Book._meta.apps.ready = True
|