1. from django import forms
    
  2. from django.contrib import admin
    
  3. from django.contrib.admin import AdminSite
    
  4. from django.contrib.auth.backends import ModelBackend
    
  5. from django.contrib.auth.middleware import AuthenticationMiddleware
    
  6. from django.contrib.contenttypes.admin import GenericStackedInline
    
  7. from django.contrib.messages.middleware import MessageMiddleware
    
  8. from django.contrib.sessions.middleware import SessionMiddleware
    
  9. from django.core import checks
    
  10. from django.test import SimpleTestCase, override_settings
    
  11. 
    
  12. from .models import Album, Author, Book, City, Influence, Song, State, TwoAlbumFKAndAnE
    
  13. 
    
  14. 
    
  15. class SongForm(forms.ModelForm):
    
  16.     pass
    
  17. 
    
  18. 
    
  19. class ValidFields(admin.ModelAdmin):
    
  20.     form = SongForm
    
  21.     fields = ["title"]
    
  22. 
    
  23. 
    
  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)
    
  28. 
    
  29.         return ExtraFieldForm
    
  30. 
    
  31.     fieldsets = (
    
  32.         (
    
  33.             None,
    
  34.             {
    
  35.                 "fields": ("name",),
    
  36.             },
    
  37.         ),
    
  38.     )
    
  39. 
    
  40. 
    
  41. class MyAdmin(admin.ModelAdmin):
    
  42.     def check(self, **kwargs):
    
  43.         return ["error!"]
    
  44. 
    
  45. 
    
  46. class AuthenticationMiddlewareSubclass(AuthenticationMiddleware):
    
  47.     pass
    
  48. 
    
  49. 
    
  50. class MessageMiddlewareSubclass(MessageMiddleware):
    
  51.     pass
    
  52. 
    
  53. 
    
  54. class ModelBackendSubclass(ModelBackend):
    
  55.     pass
    
  56. 
    
  57. 
    
  58. class SessionMiddlewareSubclass(SessionMiddleware):
    
  59.     pass
    
  60. 
    
  61. 
    
  62. @override_settings(
    
  63.     SILENCED_SYSTEM_CHECKS=["fields.W342"],  # ForeignKey(unique=True)
    
  64.     INSTALLED_APPS=[
    
  65.         "django.contrib.admin",
    
  66.         "django.contrib.auth",
    
  67.         "django.contrib.contenttypes",
    
  68.         "django.contrib.messages",
    
  69.         "admin_checks",
    
  70.     ],
    
  71. )
    
  72. class SystemChecksTestCase(SimpleTestCase):
    
  73.     databases = "__all__"
    
  74. 
    
  75.     def test_checks_are_performed(self):
    
  76.         admin.site.register(Song, MyAdmin)
    
  77.         try:
    
  78.             errors = checks.run_checks()
    
  79.             expected = ["error!"]
    
  80.             self.assertEqual(errors, expected)
    
  81.         finally:
    
  82.             admin.site.unregister(Song)
    
  83. 
    
  84.     @override_settings(INSTALLED_APPS=["django.contrib.admin"])
    
  85.     def test_apps_dependencies(self):
    
  86.         errors = admin.checks.check_dependencies()
    
  87.         expected = [
    
  88.             checks.Error(
    
  89.                 "'django.contrib.contenttypes' must be in "
    
  90.                 "INSTALLED_APPS in order to use the admin application.",
    
  91.                 id="admin.E401",
    
  92.             ),
    
  93.             checks.Error(
    
  94.                 "'django.contrib.auth' must be in INSTALLED_APPS in order "
    
  95.                 "to use the admin application.",
    
  96.                 id="admin.E405",
    
  97.             ),
    
  98.             checks.Error(
    
  99.                 "'django.contrib.messages' must be in INSTALLED_APPS in order "
    
  100.                 "to use the admin application.",
    
  101.                 id="admin.E406",
    
  102.             ),
    
  103.         ]
    
  104.         self.assertEqual(errors, expected)
    
  105. 
    
  106.     @override_settings(TEMPLATES=[])
    
  107.     def test_no_template_engines(self):
    
  108.         self.assertEqual(
    
  109.             admin.checks.check_dependencies(),
    
  110.             [
    
  111.                 checks.Error(
    
  112.                     "A 'django.template.backends.django.DjangoTemplates' "
    
  113.                     "instance must be configured in TEMPLATES in order to use "
    
  114.                     "the admin application.",
    
  115.                     id="admin.E403",
    
  116.                 )
    
  117.             ],
    
  118.         )
    
  119. 
    
  120.     @override_settings(
    
  121.         TEMPLATES=[
    
  122.             {
    
  123.                 "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  124.                 "DIRS": [],
    
  125.                 "APP_DIRS": True,
    
  126.                 "OPTIONS": {
    
  127.                     "context_processors": [],
    
  128.                 },
    
  129.             }
    
  130.         ],
    
  131.     )
    
  132.     def test_context_processor_dependencies(self):
    
  133.         expected = [
    
  134.             checks.Error(
    
  135.                 "'django.contrib.auth.context_processors.auth' must be "
    
  136.                 "enabled in DjangoTemplates (TEMPLATES) if using the default "
    
  137.                 "auth backend in order to use the admin application.",
    
  138.                 id="admin.E402",
    
  139.             ),
    
  140.             checks.Error(
    
  141.                 "'django.contrib.messages.context_processors.messages' must "
    
  142.                 "be enabled in DjangoTemplates (TEMPLATES) in order to use "
    
  143.                 "the admin application.",
    
  144.                 id="admin.E404",
    
  145.             ),
    
  146.             checks.Warning(
    
  147.                 "'django.template.context_processors.request' must be enabled "
    
  148.                 "in DjangoTemplates (TEMPLATES) in order to use the admin "
    
  149.                 "navigation sidebar.",
    
  150.                 id="admin.W411",
    
  151.             ),
    
  152.         ]
    
  153.         self.assertEqual(admin.checks.check_dependencies(), expected)
    
  154.         # The first error doesn't happen if
    
  155.         # 'django.contrib.auth.backends.ModelBackend' isn't in
    
  156.         # AUTHENTICATION_BACKENDS.
    
  157.         with self.settings(AUTHENTICATION_BACKENDS=[]):
    
  158.             self.assertEqual(admin.checks.check_dependencies(), expected[1:])
    
  159. 
    
  160.     @override_settings(
    
  161.         AUTHENTICATION_BACKENDS=["admin_checks.tests.ModelBackendSubclass"],
    
  162.         TEMPLATES=[
    
  163.             {
    
  164.                 "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  165.                 "DIRS": [],
    
  166.                 "APP_DIRS": True,
    
  167.                 "OPTIONS": {
    
  168.                     "context_processors": [
    
  169.                         "django.template.context_processors.request",
    
  170.                         "django.contrib.messages.context_processors.messages",
    
  171.                     ],
    
  172.                 },
    
  173.             }
    
  174.         ],
    
  175.     )
    
  176.     def test_context_processor_dependencies_model_backend_subclass(self):
    
  177.         self.assertEqual(
    
  178.             admin.checks.check_dependencies(),
    
  179.             [
    
  180.                 checks.Error(
    
  181.                     "'django.contrib.auth.context_processors.auth' must be "
    
  182.                     "enabled in DjangoTemplates (TEMPLATES) if using the default "
    
  183.                     "auth backend in order to use the admin application.",
    
  184.                     id="admin.E402",
    
  185.                 ),
    
  186.             ],
    
  187.         )
    
  188. 
    
  189.     @override_settings(
    
  190.         TEMPLATES=[
    
  191.             {
    
  192.                 "BACKEND": "django.template.backends.dummy.TemplateStrings",
    
  193.                 "DIRS": [],
    
  194.                 "APP_DIRS": True,
    
  195.             },
    
  196.             {
    
  197.                 "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  198.                 "DIRS": [],
    
  199.                 "APP_DIRS": True,
    
  200.                 "OPTIONS": {
    
  201.                     "context_processors": [
    
  202.                         "django.template.context_processors.request",
    
  203.                         "django.contrib.auth.context_processors.auth",
    
  204.                         "django.contrib.messages.context_processors.messages",
    
  205.                     ],
    
  206.                 },
    
  207.             },
    
  208.         ],
    
  209.     )
    
  210.     def test_several_templates_backends(self):
    
  211.         self.assertEqual(admin.checks.check_dependencies(), [])
    
  212. 
    
  213.     @override_settings(MIDDLEWARE=[])
    
  214.     def test_middleware_dependencies(self):
    
  215.         errors = admin.checks.check_dependencies()
    
  216.         expected = [
    
  217.             checks.Error(
    
  218.                 "'django.contrib.auth.middleware.AuthenticationMiddleware' "
    
  219.                 "must be in MIDDLEWARE in order to use the admin application.",
    
  220.                 id="admin.E408",
    
  221.             ),
    
  222.             checks.Error(
    
  223.                 "'django.contrib.messages.middleware.MessageMiddleware' "
    
  224.                 "must be in MIDDLEWARE in order to use the admin application.",
    
  225.                 id="admin.E409",
    
  226.             ),
    
  227.             checks.Error(
    
  228.                 "'django.contrib.sessions.middleware.SessionMiddleware' "
    
  229.                 "must be in MIDDLEWARE in order to use the admin application.",
    
  230.                 hint=(
    
  231.                     "Insert "
    
  232.                     "'django.contrib.sessions.middleware.SessionMiddleware' "
    
  233.                     "before "
    
  234.                     "'django.contrib.auth.middleware.AuthenticationMiddleware'."
    
  235.                 ),
    
  236.                 id="admin.E410",
    
  237.             ),
    
  238.         ]
    
  239.         self.assertEqual(errors, expected)
    
  240. 
    
  241.     @override_settings(
    
  242.         MIDDLEWARE=[
    
  243.             "admin_checks.tests.AuthenticationMiddlewareSubclass",
    
  244.             "admin_checks.tests.MessageMiddlewareSubclass",
    
  245.             "admin_checks.tests.SessionMiddlewareSubclass",
    
  246.         ]
    
  247.     )
    
  248.     def test_middleware_subclasses(self):
    
  249.         self.assertEqual(admin.checks.check_dependencies(), [])
    
  250. 
    
  251.     @override_settings(
    
  252.         MIDDLEWARE=[
    
  253.             "django.contrib.does.not.Exist",
    
  254.             "django.contrib.auth.middleware.AuthenticationMiddleware",
    
  255.             "django.contrib.messages.middleware.MessageMiddleware",
    
  256.             "django.contrib.sessions.middleware.SessionMiddleware",
    
  257.         ]
    
  258.     )
    
  259.     def test_admin_check_ignores_import_error_in_middleware(self):
    
  260.         self.assertEqual(admin.checks.check_dependencies(), [])
    
  261. 
    
  262.     def test_custom_adminsite(self):
    
  263.         class CustomAdminSite(admin.AdminSite):
    
  264.             pass
    
  265. 
    
  266.         custom_site = CustomAdminSite()
    
  267.         custom_site.register(Song, MyAdmin)
    
  268.         try:
    
  269.             errors = checks.run_checks()
    
  270.             expected = ["error!"]
    
  271.             self.assertEqual(errors, expected)
    
  272.         finally:
    
  273.             custom_site.unregister(Song)
    
  274. 
    
  275.     def test_allows_checks_relying_on_other_modeladmins(self):
    
  276.         class MyBookAdmin(admin.ModelAdmin):
    
  277.             def check(self, **kwargs):
    
  278.                 errors = super().check(**kwargs)
    
  279.                 author_admin = self.admin_site._registry.get(Author)
    
  280.                 if author_admin is None:
    
  281.                     errors.append("AuthorAdmin missing!")
    
  282.                 return errors
    
  283. 
    
  284.         class MyAuthorAdmin(admin.ModelAdmin):
    
  285.             pass
    
  286. 
    
  287.         admin.site.register(Book, MyBookAdmin)
    
  288.         admin.site.register(Author, MyAuthorAdmin)
    
  289.         try:
    
  290.             self.assertEqual(admin.site.check(None), [])
    
  291.         finally:
    
  292.             admin.site.unregister(Book)
    
  293.             admin.site.unregister(Author)
    
  294. 
    
  295.     def test_field_name_not_in_list_display(self):
    
  296.         class SongAdmin(admin.ModelAdmin):
    
  297.             list_editable = ["original_release"]
    
  298. 
    
  299.         errors = SongAdmin(Song, AdminSite()).check()
    
  300.         expected = [
    
  301.             checks.Error(
    
  302.                 "The value of 'list_editable[0]' refers to 'original_release', "
    
  303.                 "which is not contained in 'list_display'.",
    
  304.                 obj=SongAdmin,
    
  305.                 id="admin.E122",
    
  306.             )
    
  307.         ]
    
  308.         self.assertEqual(errors, expected)
    
  309. 
    
  310.     def test_list_editable_not_a_list_or_tuple(self):
    
  311.         class SongAdmin(admin.ModelAdmin):
    
  312.             list_editable = "test"
    
  313. 
    
  314.         self.assertEqual(
    
  315.             SongAdmin(Song, AdminSite()).check(),
    
  316.             [
    
  317.                 checks.Error(
    
  318.                     "The value of 'list_editable' must be a list or tuple.",
    
  319.                     obj=SongAdmin,
    
  320.                     id="admin.E120",
    
  321.                 )
    
  322.             ],
    
  323.         )
    
  324. 
    
  325.     def test_list_editable_missing_field(self):
    
  326.         class SongAdmin(admin.ModelAdmin):
    
  327.             list_editable = ("test",)
    
  328. 
    
  329.         self.assertEqual(
    
  330.             SongAdmin(Song, AdminSite()).check(),
    
  331.             [
    
  332.                 checks.Error(
    
  333.                     "The value of 'list_editable[0]' refers to 'test', which is "
    
  334.                     "not a field of 'admin_checks.Song'.",
    
  335.                     obj=SongAdmin,
    
  336.                     id="admin.E121",
    
  337.                 )
    
  338.             ],
    
  339.         )
    
  340. 
    
  341.     def test_readonly_and_editable(self):
    
  342.         class SongAdmin(admin.ModelAdmin):
    
  343.             readonly_fields = ["original_release"]
    
  344.             list_display = ["pk", "original_release"]
    
  345.             list_editable = ["original_release"]
    
  346.             fieldsets = [
    
  347.                 (
    
  348.                     None,
    
  349.                     {
    
  350.                         "fields": ["title", "original_release"],
    
  351.                     },
    
  352.                 ),
    
  353.             ]
    
  354. 
    
  355.         errors = SongAdmin(Song, AdminSite()).check()
    
  356.         expected = [
    
  357.             checks.Error(
    
  358.                 "The value of 'list_editable[0]' refers to 'original_release', "
    
  359.                 "which is not editable through the admin.",
    
  360.                 obj=SongAdmin,
    
  361.                 id="admin.E125",
    
  362.             )
    
  363.         ]
    
  364.         self.assertEqual(errors, expected)
    
  365. 
    
  366.     def test_pk_not_editable(self):
    
  367.         # PKs cannot be edited in the list.
    
  368.         class SongAdmin(admin.ModelAdmin):
    
  369.             list_display = ["title", "id"]
    
  370.             list_editable = ["id"]
    
  371. 
    
  372.         errors = SongAdmin(Song, AdminSite()).check()
    
  373.         expected = [
    
  374.             checks.Error(
    
  375.                 "The value of 'list_editable[0]' refers to 'id', which is not editable "
    
  376.                 "through the admin.",
    
  377.                 obj=SongAdmin,
    
  378.                 id="admin.E125",
    
  379.             )
    
  380.         ]
    
  381.         self.assertEqual(errors, expected)
    
  382. 
    
  383.     def test_editable(self):
    
  384.         class SongAdmin(admin.ModelAdmin):
    
  385.             list_display = ["pk", "title"]
    
  386.             list_editable = ["title"]
    
  387.             fieldsets = [
    
  388.                 (
    
  389.                     None,
    
  390.                     {
    
  391.                         "fields": ["title", "original_release"],
    
  392.                     },
    
  393.                 ),
    
  394.             ]
    
  395. 
    
  396.         errors = SongAdmin(Song, AdminSite()).check()
    
  397.         self.assertEqual(errors, [])
    
  398. 
    
  399.     def test_custom_modelforms_with_fields_fieldsets(self):
    
  400.         """
    
  401.         # Regression test for #8027: custom ModelForms with fields/fieldsets
    
  402.         """
    
  403.         errors = ValidFields(Song, AdminSite()).check()
    
  404.         self.assertEqual(errors, [])
    
  405. 
    
  406.     def test_custom_get_form_with_fieldsets(self):
    
  407.         """
    
  408.         The fieldsets checks are skipped when the ModelAdmin.get_form() method
    
  409.         is overridden.
    
  410.         """
    
  411.         errors = ValidFormFieldsets(Song, AdminSite()).check()
    
  412.         self.assertEqual(errors, [])
    
  413. 
    
  414.     def test_fieldsets_fields_non_tuple(self):
    
  415.         """
    
  416.         The first fieldset's fields must be a list/tuple.
    
  417.         """
    
  418. 
    
  419.         class NotATupleAdmin(admin.ModelAdmin):
    
  420.             list_display = ["pk", "title"]
    
  421.             list_editable = ["title"]
    
  422.             fieldsets = [
    
  423.                 (None, {"fields": "title"}),  # not a tuple
    
  424.             ]
    
  425. 
    
  426.         errors = NotATupleAdmin(Song, AdminSite()).check()
    
  427.         expected = [
    
  428.             checks.Error(
    
  429.                 "The value of 'fieldsets[0][1]['fields']' must be a list or tuple.",
    
  430.                 obj=NotATupleAdmin,
    
  431.                 id="admin.E008",
    
  432.             )
    
  433.         ]
    
  434.         self.assertEqual(errors, expected)
    
  435. 
    
  436.     def test_nonfirst_fieldset(self):
    
  437.         """
    
  438.         The second fieldset's fields must be a list/tuple.
    
  439.         """
    
  440. 
    
  441.         class NotATupleAdmin(admin.ModelAdmin):
    
  442.             fieldsets = [
    
  443.                 (None, {"fields": ("title",)}),
    
  444.                 ("foo", {"fields": "author"}),  # not a tuple
    
  445.             ]
    
  446. 
    
  447.         errors = NotATupleAdmin(Song, AdminSite()).check()
    
  448.         expected = [
    
  449.             checks.Error(
    
  450.                 "The value of 'fieldsets[1][1]['fields']' must be a list or tuple.",
    
  451.                 obj=NotATupleAdmin,
    
  452.                 id="admin.E008",
    
  453.             )
    
  454.         ]
    
  455.         self.assertEqual(errors, expected)
    
  456. 
    
  457.     def test_exclude_values(self):
    
  458.         """
    
  459.         Tests for basic system checks of 'exclude' option values (#12689)
    
  460.         """
    
  461. 
    
  462.         class ExcludedFields1(admin.ModelAdmin):
    
  463.             exclude = "foo"
    
  464. 
    
  465.         errors = ExcludedFields1(Book, AdminSite()).check()
    
  466.         expected = [
    
  467.             checks.Error(
    
  468.                 "The value of 'exclude' must be a list or tuple.",
    
  469.                 obj=ExcludedFields1,
    
  470.                 id="admin.E014",
    
  471.             )
    
  472.         ]
    
  473.         self.assertEqual(errors, expected)
    
  474. 
    
  475.     def test_exclude_duplicate_values(self):
    
  476.         class ExcludedFields2(admin.ModelAdmin):
    
  477.             exclude = ("name", "name")
    
  478. 
    
  479.         errors = ExcludedFields2(Book, AdminSite()).check()
    
  480.         expected = [
    
  481.             checks.Error(
    
  482.                 "The value of 'exclude' contains duplicate field(s).",
    
  483.                 obj=ExcludedFields2,
    
  484.                 id="admin.E015",
    
  485.             )
    
  486.         ]
    
  487.         self.assertEqual(errors, expected)
    
  488. 
    
  489.     def test_exclude_in_inline(self):
    
  490.         class ExcludedFieldsInline(admin.TabularInline):
    
  491.             model = Song
    
  492.             exclude = "foo"
    
  493. 
    
  494.         class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
    
  495.             model = Album
    
  496.             inlines = [ExcludedFieldsInline]
    
  497. 
    
  498.         errors = ExcludedFieldsAlbumAdmin(Album, AdminSite()).check()
    
  499.         expected = [
    
  500.             checks.Error(
    
  501.                 "The value of 'exclude' must be a list or tuple.",
    
  502.                 obj=ExcludedFieldsInline,
    
  503.                 id="admin.E014",
    
  504.             )
    
  505.         ]
    
  506.         self.assertEqual(errors, expected)
    
  507. 
    
  508.     def test_exclude_inline_model_admin(self):
    
  509.         """
    
  510.         Regression test for #9932 - exclude in InlineModelAdmin should not
    
  511.         contain the ForeignKey field used in ModelAdmin.model
    
  512.         """
    
  513. 
    
  514.         class SongInline(admin.StackedInline):
    
  515.             model = Song
    
  516.             exclude = ["album"]
    
  517. 
    
  518.         class AlbumAdmin(admin.ModelAdmin):
    
  519.             model = Album
    
  520.             inlines = [SongInline]
    
  521. 
    
  522.         errors = AlbumAdmin(Album, AdminSite()).check()
    
  523.         expected = [
    
  524.             checks.Error(
    
  525.                 "Cannot exclude the field 'album', because it is the foreign key "
    
  526.                 "to the parent model 'admin_checks.Album'.",
    
  527.                 obj=SongInline,
    
  528.                 id="admin.E201",
    
  529.             )
    
  530.         ]
    
  531.         self.assertEqual(errors, expected)
    
  532. 
    
  533.     def test_valid_generic_inline_model_admin(self):
    
  534.         """
    
  535.         Regression test for #22034 - check that generic inlines don't look for
    
  536.         normal ForeignKey relations.
    
  537.         """
    
  538. 
    
  539.         class InfluenceInline(GenericStackedInline):
    
  540.             model = Influence
    
  541. 
    
  542.         class SongAdmin(admin.ModelAdmin):
    
  543.             inlines = [InfluenceInline]
    
  544. 
    
  545.         errors = SongAdmin(Song, AdminSite()).check()
    
  546.         self.assertEqual(errors, [])
    
  547. 
    
  548.     def test_generic_inline_model_admin_non_generic_model(self):
    
  549.         """
    
  550.         A model without a GenericForeignKey raises problems if it's included
    
  551.         in a GenericInlineModelAdmin definition.
    
  552.         """
    
  553. 
    
  554.         class BookInline(GenericStackedInline):
    
  555.             model = Book
    
  556. 
    
  557.         class SongAdmin(admin.ModelAdmin):
    
  558.             inlines = [BookInline]
    
  559. 
    
  560.         errors = SongAdmin(Song, AdminSite()).check()
    
  561.         expected = [
    
  562.             checks.Error(
    
  563.                 "'admin_checks.Book' has no GenericForeignKey.",
    
  564.                 obj=BookInline,
    
  565.                 id="admin.E301",
    
  566.             )
    
  567.         ]
    
  568.         self.assertEqual(errors, expected)
    
  569. 
    
  570.     def test_generic_inline_model_admin_bad_ct_field(self):
    
  571.         """
    
  572.         A GenericInlineModelAdmin errors if the ct_field points to a
    
  573.         nonexistent field.
    
  574.         """
    
  575. 
    
  576.         class InfluenceInline(GenericStackedInline):
    
  577.             model = Influence
    
  578.             ct_field = "nonexistent"
    
  579. 
    
  580.         class SongAdmin(admin.ModelAdmin):
    
  581.             inlines = [InfluenceInline]
    
  582. 
    
  583.         errors = SongAdmin(Song, AdminSite()).check()
    
  584.         expected = [
    
  585.             checks.Error(
    
  586.                 "'ct_field' references 'nonexistent', which is not a field on "
    
  587.                 "'admin_checks.Influence'.",
    
  588.                 obj=InfluenceInline,
    
  589.                 id="admin.E302",
    
  590.             )
    
  591.         ]
    
  592.         self.assertEqual(errors, expected)
    
  593. 
    
  594.     def test_generic_inline_model_admin_bad_fk_field(self):
    
  595.         """
    
  596.         A GenericInlineModelAdmin errors if the ct_fk_field points to a
    
  597.         nonexistent field.
    
  598.         """
    
  599. 
    
  600.         class InfluenceInline(GenericStackedInline):
    
  601.             model = Influence
    
  602.             ct_fk_field = "nonexistent"
    
  603. 
    
  604.         class SongAdmin(admin.ModelAdmin):
    
  605.             inlines = [InfluenceInline]
    
  606. 
    
  607.         errors = SongAdmin(Song, AdminSite()).check()
    
  608.         expected = [
    
  609.             checks.Error(
    
  610.                 "'ct_fk_field' references 'nonexistent', which is not a field on "
    
  611.                 "'admin_checks.Influence'.",
    
  612.                 obj=InfluenceInline,
    
  613.                 id="admin.E303",
    
  614.             )
    
  615.         ]
    
  616.         self.assertEqual(errors, expected)
    
  617. 
    
  618.     def test_generic_inline_model_admin_non_gfk_ct_field(self):
    
  619.         """
    
  620.         A GenericInlineModelAdmin raises problems if the ct_field points to a
    
  621.         field that isn't part of a GenericForeignKey.
    
  622.         """
    
  623. 
    
  624.         class InfluenceInline(GenericStackedInline):
    
  625.             model = Influence
    
  626.             ct_field = "name"
    
  627. 
    
  628.         class SongAdmin(admin.ModelAdmin):
    
  629.             inlines = [InfluenceInline]
    
  630. 
    
  631.         errors = SongAdmin(Song, AdminSite()).check()
    
  632.         expected = [
    
  633.             checks.Error(
    
  634.                 "'admin_checks.Influence' has no GenericForeignKey using "
    
  635.                 "content type field 'name' and object ID field 'object_id'.",
    
  636.                 obj=InfluenceInline,
    
  637.                 id="admin.E304",
    
  638.             )
    
  639.         ]
    
  640.         self.assertEqual(errors, expected)
    
  641. 
    
  642.     def test_generic_inline_model_admin_non_gfk_fk_field(self):
    
  643.         """
    
  644.         A GenericInlineModelAdmin raises problems if the ct_fk_field points to
    
  645.         a field that isn't part of a GenericForeignKey.
    
  646.         """
    
  647. 
    
  648.         class InfluenceInline(GenericStackedInline):
    
  649.             model = Influence
    
  650.             ct_fk_field = "name"
    
  651. 
    
  652.         class SongAdmin(admin.ModelAdmin):
    
  653.             inlines = [InfluenceInline]
    
  654. 
    
  655.         errors = SongAdmin(Song, AdminSite()).check()
    
  656.         expected = [
    
  657.             checks.Error(
    
  658.                 "'admin_checks.Influence' has no GenericForeignKey using "
    
  659.                 "content type field 'content_type' and object ID field 'name'.",
    
  660.                 obj=InfluenceInline,
    
  661.                 id="admin.E304",
    
  662.             )
    
  663.         ]
    
  664.         self.assertEqual(errors, expected)
    
  665. 
    
  666.     def test_app_label_in_admin_checks(self):
    
  667.         class RawIdNonexistentAdmin(admin.ModelAdmin):
    
  668.             raw_id_fields = ("nonexistent",)
    
  669. 
    
  670.         errors = RawIdNonexistentAdmin(Album, AdminSite()).check()
    
  671.         expected = [
    
  672.             checks.Error(
    
  673.                 "The value of 'raw_id_fields[0]' refers to 'nonexistent', "
    
  674.                 "which is not a field of 'admin_checks.Album'.",
    
  675.                 obj=RawIdNonexistentAdmin,
    
  676.                 id="admin.E002",
    
  677.             )
    
  678.         ]
    
  679.         self.assertEqual(errors, expected)
    
  680. 
    
  681.     def test_fk_exclusion(self):
    
  682.         """
    
  683.         Regression test for #11709 - when testing for fk excluding (when exclude is
    
  684.         given) make sure fk_name is honored or things blow up when there is more
    
  685.         than one fk to the parent model.
    
  686.         """
    
  687. 
    
  688.         class TwoAlbumFKAndAnEInline(admin.TabularInline):
    
  689.             model = TwoAlbumFKAndAnE
    
  690.             exclude = ("e",)
    
  691.             fk_name = "album1"
    
  692. 
    
  693.         class MyAdmin(admin.ModelAdmin):
    
  694.             inlines = [TwoAlbumFKAndAnEInline]
    
  695. 
    
  696.         errors = MyAdmin(Album, AdminSite()).check()
    
  697.         self.assertEqual(errors, [])
    
  698. 
    
  699.     def test_inline_self_check(self):
    
  700.         class TwoAlbumFKAndAnEInline(admin.TabularInline):
    
  701.             model = TwoAlbumFKAndAnE
    
  702. 
    
  703.         class MyAdmin(admin.ModelAdmin):
    
  704.             inlines = [TwoAlbumFKAndAnEInline]
    
  705. 
    
  706.         errors = MyAdmin(Album, AdminSite()).check()
    
  707.         expected = [
    
  708.             checks.Error(
    
  709.                 "'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey "
    
  710.                 "to 'admin_checks.Album'. You must specify a 'fk_name' "
    
  711.                 "attribute.",
    
  712.                 obj=TwoAlbumFKAndAnEInline,
    
  713.                 id="admin.E202",
    
  714.             )
    
  715.         ]
    
  716.         self.assertEqual(errors, expected)
    
  717. 
    
  718.     def test_inline_with_specified(self):
    
  719.         class TwoAlbumFKAndAnEInline(admin.TabularInline):
    
  720.             model = TwoAlbumFKAndAnE
    
  721.             fk_name = "album1"
    
  722. 
    
  723.         class MyAdmin(admin.ModelAdmin):
    
  724.             inlines = [TwoAlbumFKAndAnEInline]
    
  725. 
    
  726.         errors = MyAdmin(Album, AdminSite()).check()
    
  727.         self.assertEqual(errors, [])
    
  728. 
    
  729.     def test_inlines_property(self):
    
  730.         class CitiesInline(admin.TabularInline):
    
  731.             model = City
    
  732. 
    
  733.         class StateAdmin(admin.ModelAdmin):
    
  734.             @property
    
  735.             def inlines(self):
    
  736.                 return [CitiesInline]
    
  737. 
    
  738.         errors = StateAdmin(State, AdminSite()).check()
    
  739.         self.assertEqual(errors, [])
    
  740. 
    
  741.     def test_readonly(self):
    
  742.         class SongAdmin(admin.ModelAdmin):
    
  743.             readonly_fields = ("title",)
    
  744. 
    
  745.         errors = SongAdmin(Song, AdminSite()).check()
    
  746.         self.assertEqual(errors, [])
    
  747. 
    
  748.     def test_readonly_on_method(self):
    
  749.         @admin.display
    
  750.         def my_function(obj):
    
  751.             pass
    
  752. 
    
  753.         class SongAdmin(admin.ModelAdmin):
    
  754.             readonly_fields = (my_function,)
    
  755. 
    
  756.         errors = SongAdmin(Song, AdminSite()).check()
    
  757.         self.assertEqual(errors, [])
    
  758. 
    
  759.     def test_readonly_on_modeladmin(self):
    
  760.         class SongAdmin(admin.ModelAdmin):
    
  761.             readonly_fields = ("readonly_method_on_modeladmin",)
    
  762. 
    
  763.             @admin.display
    
  764.             def readonly_method_on_modeladmin(self, obj):
    
  765.                 pass
    
  766. 
    
  767.         errors = SongAdmin(Song, AdminSite()).check()
    
  768.         self.assertEqual(errors, [])
    
  769. 
    
  770.     def test_readonly_dynamic_attribute_on_modeladmin(self):
    
  771.         class SongAdmin(admin.ModelAdmin):
    
  772.             readonly_fields = ("dynamic_method",)
    
  773. 
    
  774.             def __getattr__(self, item):
    
  775.                 if item == "dynamic_method":
    
  776. 
    
  777.                     @admin.display
    
  778.                     def method(obj):
    
  779.                         pass
    
  780. 
    
  781.                     return method
    
  782.                 raise AttributeError
    
  783. 
    
  784.         errors = SongAdmin(Song, AdminSite()).check()
    
  785.         self.assertEqual(errors, [])
    
  786. 
    
  787.     def test_readonly_method_on_model(self):
    
  788.         class SongAdmin(admin.ModelAdmin):
    
  789.             readonly_fields = ("readonly_method_on_model",)
    
  790. 
    
  791.         errors = SongAdmin(Song, AdminSite()).check()
    
  792.         self.assertEqual(errors, [])
    
  793. 
    
  794.     def test_nonexistent_field(self):
    
  795.         class SongAdmin(admin.ModelAdmin):
    
  796.             readonly_fields = ("title", "nonexistent")
    
  797. 
    
  798.         errors = SongAdmin(Song, AdminSite()).check()
    
  799.         expected = [
    
  800.             checks.Error(
    
  801.                 "The value of 'readonly_fields[1]' is not a callable, an attribute "
    
  802.                 "of 'SongAdmin', or an attribute of 'admin_checks.Song'.",
    
  803.                 obj=SongAdmin,
    
  804.                 id="admin.E035",
    
  805.             )
    
  806.         ]
    
  807.         self.assertEqual(errors, expected)
    
  808. 
    
  809.     def test_nonexistent_field_on_inline(self):
    
  810.         class CityInline(admin.TabularInline):
    
  811.             model = City
    
  812.             readonly_fields = ["i_dont_exist"]  # Missing attribute
    
  813. 
    
  814.         errors = CityInline(State, AdminSite()).check()
    
  815.         expected = [
    
  816.             checks.Error(
    
  817.                 "The value of 'readonly_fields[0]' is not a callable, an attribute "
    
  818.                 "of 'CityInline', or an attribute of 'admin_checks.City'.",
    
  819.                 obj=CityInline,
    
  820.                 id="admin.E035",
    
  821.             )
    
  822.         ]
    
  823.         self.assertEqual(errors, expected)
    
  824. 
    
  825.     def test_readonly_fields_not_list_or_tuple(self):
    
  826.         class SongAdmin(admin.ModelAdmin):
    
  827.             readonly_fields = "test"
    
  828. 
    
  829.         self.assertEqual(
    
  830.             SongAdmin(Song, AdminSite()).check(),
    
  831.             [
    
  832.                 checks.Error(
    
  833.                     "The value of 'readonly_fields' must be a list or tuple.",
    
  834.                     obj=SongAdmin,
    
  835.                     id="admin.E034",
    
  836.                 )
    
  837.             ],
    
  838.         )
    
  839. 
    
  840.     def test_extra(self):
    
  841.         class SongAdmin(admin.ModelAdmin):
    
  842.             @admin.display
    
  843.             def awesome_song(self, instance):
    
  844.                 if instance.title == "Born to Run":
    
  845.                     return "Best Ever!"
    
  846.                 return "Status unknown."
    
  847. 
    
  848.         errors = SongAdmin(Song, AdminSite()).check()
    
  849.         self.assertEqual(errors, [])
    
  850. 
    
  851.     def test_readonly_lambda(self):
    
  852.         class SongAdmin(admin.ModelAdmin):
    
  853.             readonly_fields = (lambda obj: "test",)
    
  854. 
    
  855.         errors = SongAdmin(Song, AdminSite()).check()
    
  856.         self.assertEqual(errors, [])
    
  857. 
    
  858.     def test_graceful_m2m_fail(self):
    
  859.         """
    
  860.         Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
    
  861.         specifies the 'through' option is included in the 'fields' or the 'fieldsets'
    
  862.         ModelAdmin options.
    
  863.         """
    
  864. 
    
  865.         class BookAdmin(admin.ModelAdmin):
    
  866.             fields = ["authors"]
    
  867. 
    
  868.         errors = BookAdmin(Book, AdminSite()).check()
    
  869.         expected = [
    
  870.             checks.Error(
    
  871.                 "The value of 'fields' cannot include the ManyToManyField 'authors', "
    
  872.                 "because that field manually specifies a relationship model.",
    
  873.                 obj=BookAdmin,
    
  874.                 id="admin.E013",
    
  875.             )
    
  876.         ]
    
  877.         self.assertEqual(errors, expected)
    
  878. 
    
  879.     def test_cannot_include_through(self):
    
  880.         class FieldsetBookAdmin(admin.ModelAdmin):
    
  881.             fieldsets = (
    
  882.                 ("Header 1", {"fields": ("name",)}),
    
  883.                 ("Header 2", {"fields": ("authors",)}),
    
  884.             )
    
  885. 
    
  886.         errors = FieldsetBookAdmin(Book, AdminSite()).check()
    
  887.         expected = [
    
  888.             checks.Error(
    
  889.                 "The value of 'fieldsets[1][1][\"fields\"]' cannot include the "
    
  890.                 "ManyToManyField 'authors', because that field manually specifies a "
    
  891.                 "relationship model.",
    
  892.                 obj=FieldsetBookAdmin,
    
  893.                 id="admin.E013",
    
  894.             )
    
  895.         ]
    
  896.         self.assertEqual(errors, expected)
    
  897. 
    
  898.     def test_nested_fields(self):
    
  899.         class NestedFieldsAdmin(admin.ModelAdmin):
    
  900.             fields = ("price", ("name", "subtitle"))
    
  901. 
    
  902.         errors = NestedFieldsAdmin(Book, AdminSite()).check()
    
  903.         self.assertEqual(errors, [])
    
  904. 
    
  905.     def test_nested_fieldsets(self):
    
  906.         class NestedFieldsetAdmin(admin.ModelAdmin):
    
  907.             fieldsets = (("Main", {"fields": ("price", ("name", "subtitle"))}),)
    
  908. 
    
  909.         errors = NestedFieldsetAdmin(Book, AdminSite()).check()
    
  910.         self.assertEqual(errors, [])
    
  911. 
    
  912.     def test_explicit_through_override(self):
    
  913.         """
    
  914.         Regression test for #12209 -- If the explicitly provided through model
    
  915.         is specified as a string, the admin should still be able use
    
  916.         Model.m2m_field.through
    
  917.         """
    
  918. 
    
  919.         class AuthorsInline(admin.TabularInline):
    
  920.             model = Book.authors.through
    
  921. 
    
  922.         class BookAdmin(admin.ModelAdmin):
    
  923.             inlines = [AuthorsInline]
    
  924. 
    
  925.         errors = BookAdmin(Book, AdminSite()).check()
    
  926.         self.assertEqual(errors, [])
    
  927. 
    
  928.     def test_non_model_fields(self):
    
  929.         """
    
  930.         Regression for ensuring ModelAdmin.fields can contain non-model fields
    
  931.         that broke with r11737
    
  932.         """
    
  933. 
    
  934.         class SongForm(forms.ModelForm):
    
  935.             extra_data = forms.CharField()
    
  936. 
    
  937.         class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
    
  938.             form = SongForm
    
  939.             fields = ["title", "extra_data"]
    
  940. 
    
  941.         errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
    
  942.         self.assertEqual(errors, [])
    
  943. 
    
  944.     def test_non_model_first_field(self):
    
  945.         """
    
  946.         Regression for ensuring ModelAdmin.field can handle first elem being a
    
  947.         non-model field (test fix for UnboundLocalError introduced with r16225).
    
  948.         """
    
  949. 
    
  950.         class SongForm(forms.ModelForm):
    
  951.             extra_data = forms.CharField()
    
  952. 
    
  953.             class Meta:
    
  954.                 model = Song
    
  955.                 fields = "__all__"
    
  956. 
    
  957.         class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
    
  958.             form = SongForm
    
  959.             fields = ["extra_data", "title"]
    
  960. 
    
  961.         errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
    
  962.         self.assertEqual(errors, [])
    
  963. 
    
  964.     def test_check_sublists_for_duplicates(self):
    
  965.         class MyModelAdmin(admin.ModelAdmin):
    
  966.             fields = ["state", ["state"]]
    
  967. 
    
  968.         errors = MyModelAdmin(Song, AdminSite()).check()
    
  969.         expected = [
    
  970.             checks.Error(
    
  971.                 "The value of 'fields' contains duplicate field(s).",
    
  972.                 obj=MyModelAdmin,
    
  973.                 id="admin.E006",
    
  974.             )
    
  975.         ]
    
  976.         self.assertEqual(errors, expected)
    
  977. 
    
  978.     def test_check_fieldset_sublists_for_duplicates(self):
    
  979.         class MyModelAdmin(admin.ModelAdmin):
    
  980.             fieldsets = [
    
  981.                 (None, {"fields": ["title", "album", ("title", "album")]}),
    
  982.             ]
    
  983. 
    
  984.         errors = MyModelAdmin(Song, AdminSite()).check()
    
  985.         expected = [
    
  986.             checks.Error(
    
  987.                 "There are duplicate field(s) in 'fieldsets[0][1]'.",
    
  988.                 obj=MyModelAdmin,
    
  989.                 id="admin.E012",
    
  990.             )
    
  991.         ]
    
  992.         self.assertEqual(errors, expected)
    
  993. 
    
  994.     def test_list_filter_works_on_through_field_even_when_apps_not_ready(self):
    
  995.         """
    
  996.         Ensure list_filter can access reverse fields even when the app registry
    
  997.         is not ready; refs #24146.
    
  998.         """
    
  999. 
    
  1000.         class BookAdminWithListFilter(admin.ModelAdmin):
    
  1001.             list_filter = ["authorsbooks__featured"]
    
  1002. 
    
  1003.         # Temporarily pretending apps are not ready yet. This issue can happen
    
  1004.         # if the value of 'list_filter' refers to a 'through__field'.
    
  1005.         Book._meta.apps.ready = False
    
  1006.         try:
    
  1007.             errors = BookAdminWithListFilter(Book, AdminSite()).check()
    
  1008.             self.assertEqual(errors, [])
    
  1009.         finally:
    
  1010.             Book._meta.apps.ready = True