tests.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from django.contrib import admin
  4. from django.contrib.admin import AdminSite
  5. from django.contrib.contenttypes.admin import GenericStackedInline
  6. from django.core import checks
  7. from django.test import SimpleTestCase, override_settings
  8. from .models import (
  9. Album, Author, Book, City, Influence, Song, State, TwoAlbumFKAndAnE,
  10. )
  11. class SongForm(forms.ModelForm):
  12. pass
  13. class ValidFields(admin.ModelAdmin):
  14. form = SongForm
  15. fields = ['title']
  16. class ValidFormFieldsets(admin.ModelAdmin):
  17. def get_form(self, request, obj=None, **kwargs):
  18. class ExtraFieldForm(SongForm):
  19. name = forms.CharField(max_length=50)
  20. return ExtraFieldForm
  21. fieldsets = (
  22. (None, {
  23. 'fields': ('name',),
  24. }),
  25. )
  26. class MyAdmin(admin.ModelAdmin):
  27. def check(self, **kwargs):
  28. return ['error!']
  29. @override_settings(
  30. SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
  31. INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes', 'admin_checks']
  32. )
  33. class SystemChecksTestCase(SimpleTestCase):
  34. def test_checks_are_performed(self):
  35. admin.site.register(Song, MyAdmin)
  36. try:
  37. errors = checks.run_checks()
  38. expected = ['error!']
  39. self.assertEqual(errors, expected)
  40. finally:
  41. admin.site.unregister(Song)
  42. @override_settings(INSTALLED_APPS=['django.contrib.admin'])
  43. def test_contenttypes_dependency(self):
  44. errors = admin.checks.check_dependencies()
  45. expected = [
  46. checks.Error(
  47. "'django.contrib.contenttypes' must be in "
  48. "INSTALLED_APPS in order to use the admin application.",
  49. id="admin.E401",
  50. )
  51. ]
  52. self.assertEqual(errors, expected)
  53. @override_settings(
  54. INSTALLED_APPS=[
  55. 'django.contrib.admin',
  56. 'django.contrib.auth',
  57. 'django.contrib.contenttypes',
  58. ],
  59. TEMPLATES=[{
  60. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  61. 'DIRS': [],
  62. 'APP_DIRS': True,
  63. 'OPTIONS': {
  64. 'context_processors': [],
  65. },
  66. }],
  67. )
  68. def test_auth_contextprocessor_dependency(self):
  69. errors = admin.checks.check_dependencies()
  70. expected = [
  71. checks.Error(
  72. "'django.contrib.auth.context_processors.auth' must be in "
  73. "TEMPLATES in order to use the admin application.",
  74. id="admin.E402",
  75. )
  76. ]
  77. self.assertEqual(errors, expected)
  78. def test_custom_adminsite(self):
  79. class CustomAdminSite(admin.AdminSite):
  80. pass
  81. custom_site = CustomAdminSite()
  82. custom_site.register(Song, MyAdmin)
  83. try:
  84. errors = checks.run_checks()
  85. expected = ['error!']
  86. self.assertEqual(errors, expected)
  87. finally:
  88. custom_site.unregister(Song)
  89. def test_allows_checks_relying_on_other_modeladmins(self):
  90. class MyBookAdmin(admin.ModelAdmin):
  91. def check(self, **kwargs):
  92. errors = super(MyBookAdmin, self).check(**kwargs)
  93. author_admin = self.admin_site._registry.get(Author)
  94. if author_admin is None:
  95. errors.append('AuthorAdmin missing!')
  96. return errors
  97. class MyAuthorAdmin(admin.ModelAdmin):
  98. pass
  99. admin.site.register(Book, MyBookAdmin)
  100. admin.site.register(Author, MyAuthorAdmin)
  101. try:
  102. self.assertEqual(admin.site.check(None), [])
  103. finally:
  104. admin.site.unregister(Book)
  105. admin.site.unregister(Author)
  106. def test_field_name_not_in_list_display(self):
  107. class SongAdmin(admin.ModelAdmin):
  108. list_editable = ["original_release"]
  109. errors = SongAdmin(Song, AdminSite()).check()
  110. expected = [
  111. checks.Error(
  112. "The value of 'list_editable[0]' refers to 'original_release', "
  113. "which is not contained in 'list_display'.",
  114. obj=SongAdmin,
  115. id='admin.E122',
  116. )
  117. ]
  118. self.assertEqual(errors, expected)
  119. def test_readonly_and_editable(self):
  120. class SongAdmin(admin.ModelAdmin):
  121. readonly_fields = ["original_release"]
  122. list_display = ["pk", "original_release"]
  123. list_editable = ["original_release"]
  124. fieldsets = [
  125. (None, {
  126. "fields": ["title", "original_release"],
  127. }),
  128. ]
  129. errors = SongAdmin(Song, AdminSite()).check()
  130. expected = [
  131. checks.Error(
  132. "The value of 'list_editable[0]' refers to 'original_release', "
  133. "which is not editable through the admin.",
  134. obj=SongAdmin,
  135. id='admin.E125',
  136. )
  137. ]
  138. self.assertEqual(errors, expected)
  139. def test_editable(self):
  140. class SongAdmin(admin.ModelAdmin):
  141. list_display = ["pk", "title"]
  142. list_editable = ["title"]
  143. fieldsets = [
  144. (None, {
  145. "fields": ["title", "original_release"],
  146. }),
  147. ]
  148. errors = SongAdmin(Song, AdminSite()).check()
  149. self.assertEqual(errors, [])
  150. def test_custom_modelforms_with_fields_fieldsets(self):
  151. """
  152. # Regression test for #8027: custom ModelForms with fields/fieldsets
  153. """
  154. errors = ValidFields(Song, AdminSite()).check()
  155. self.assertEqual(errors, [])
  156. def test_custom_get_form_with_fieldsets(self):
  157. """
  158. The fieldsets checks are skipped when the ModelAdmin.get_form() method
  159. is overridden.
  160. """
  161. errors = ValidFormFieldsets(Song, AdminSite()).check()
  162. self.assertEqual(errors, [])
  163. def test_fieldsets_fields_non_tuple(self):
  164. """
  165. The first fieldset's fields must be a list/tuple.
  166. """
  167. class NotATupleAdmin(admin.ModelAdmin):
  168. list_display = ["pk", "title"]
  169. list_editable = ["title"]
  170. fieldsets = [
  171. (None, {
  172. "fields": "title" # not a tuple
  173. }),
  174. ]
  175. errors = NotATupleAdmin(Song, AdminSite()).check()
  176. expected = [
  177. checks.Error(
  178. "The value of 'fieldsets[0][1]['fields']' must be a list or tuple.",
  179. obj=NotATupleAdmin,
  180. id='admin.E008',
  181. )
  182. ]
  183. self.assertEqual(errors, expected)
  184. def test_nonfirst_fieldset(self):
  185. """
  186. The second fieldset's fields must be a list/tuple.
  187. """
  188. class NotATupleAdmin(admin.ModelAdmin):
  189. fieldsets = [
  190. (None, {
  191. "fields": ("title",)
  192. }),
  193. ('foo', {
  194. "fields": "author" # not a tuple
  195. }),
  196. ]
  197. errors = NotATupleAdmin(Song, AdminSite()).check()
  198. expected = [
  199. checks.Error(
  200. "The value of 'fieldsets[1][1]['fields']' must be a list or tuple.",
  201. obj=NotATupleAdmin,
  202. id='admin.E008',
  203. )
  204. ]
  205. self.assertEqual(errors, expected)
  206. def test_exclude_values(self):
  207. """
  208. Tests for basic system checks of 'exclude' option values (#12689)
  209. """
  210. class ExcludedFields1(admin.ModelAdmin):
  211. exclude = 'foo'
  212. errors = ExcludedFields1(Book, AdminSite()).check()
  213. expected = [
  214. checks.Error(
  215. "The value of 'exclude' must be a list or tuple.",
  216. obj=ExcludedFields1,
  217. id='admin.E014',
  218. )
  219. ]
  220. self.assertEqual(errors, expected)
  221. def test_exclude_duplicate_values(self):
  222. class ExcludedFields2(admin.ModelAdmin):
  223. exclude = ('name', 'name')
  224. errors = ExcludedFields2(Book, AdminSite()).check()
  225. expected = [
  226. checks.Error(
  227. "The value of 'exclude' contains duplicate field(s).",
  228. obj=ExcludedFields2,
  229. id='admin.E015',
  230. )
  231. ]
  232. self.assertEqual(errors, expected)
  233. def test_exclude_in_inline(self):
  234. class ExcludedFieldsInline(admin.TabularInline):
  235. model = Song
  236. exclude = 'foo'
  237. class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
  238. model = Album
  239. inlines = [ExcludedFieldsInline]
  240. errors = ExcludedFieldsAlbumAdmin(Album, AdminSite()).check()
  241. expected = [
  242. checks.Error(
  243. "The value of 'exclude' must be a list or tuple.",
  244. obj=ExcludedFieldsInline,
  245. id='admin.E014',
  246. )
  247. ]
  248. self.assertEqual(errors, expected)
  249. def test_exclude_inline_model_admin(self):
  250. """
  251. Regression test for #9932 - exclude in InlineModelAdmin should not
  252. contain the ForeignKey field used in ModelAdmin.model
  253. """
  254. class SongInline(admin.StackedInline):
  255. model = Song
  256. exclude = ['album']
  257. class AlbumAdmin(admin.ModelAdmin):
  258. model = Album
  259. inlines = [SongInline]
  260. errors = AlbumAdmin(Album, AdminSite()).check()
  261. expected = [
  262. checks.Error(
  263. "Cannot exclude the field 'album', because it is the foreign key "
  264. "to the parent model 'admin_checks.Album'.",
  265. obj=SongInline,
  266. id='admin.E201',
  267. )
  268. ]
  269. self.assertEqual(errors, expected)
  270. def test_valid_generic_inline_model_admin(self):
  271. """
  272. Regression test for #22034 - check that generic inlines don't look for
  273. normal ForeignKey relations.
  274. """
  275. class InfluenceInline(GenericStackedInline):
  276. model = Influence
  277. class SongAdmin(admin.ModelAdmin):
  278. inlines = [InfluenceInline]
  279. errors = SongAdmin(Song, AdminSite()).check()
  280. self.assertEqual(errors, [])
  281. def test_generic_inline_model_admin_non_generic_model(self):
  282. """
  283. A model without a GenericForeignKey raises problems if it's included
  284. in an GenericInlineModelAdmin definition.
  285. """
  286. class BookInline(GenericStackedInline):
  287. model = Book
  288. class SongAdmin(admin.ModelAdmin):
  289. inlines = [BookInline]
  290. errors = SongAdmin(Song, AdminSite()).check()
  291. expected = [
  292. checks.Error(
  293. "'admin_checks.Book' has no GenericForeignKey.",
  294. obj=BookInline,
  295. id='admin.E301',
  296. )
  297. ]
  298. self.assertEqual(errors, expected)
  299. def test_generic_inline_model_admin_bad_ct_field(self):
  300. "A GenericInlineModelAdmin raises problems if the ct_field points to a non-existent field."
  301. class InfluenceInline(GenericStackedInline):
  302. model = Influence
  303. ct_field = 'nonexistent'
  304. class SongAdmin(admin.ModelAdmin):
  305. inlines = [InfluenceInline]
  306. errors = SongAdmin(Song, AdminSite()).check()
  307. expected = [
  308. checks.Error(
  309. "'ct_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
  310. obj=InfluenceInline,
  311. id='admin.E302',
  312. )
  313. ]
  314. self.assertEqual(errors, expected)
  315. def test_generic_inline_model_admin_bad_fk_field(self):
  316. "A GenericInlineModelAdmin raises problems if the ct_fk_field points to a non-existent field."
  317. class InfluenceInline(GenericStackedInline):
  318. model = Influence
  319. ct_fk_field = 'nonexistent'
  320. class SongAdmin(admin.ModelAdmin):
  321. inlines = [InfluenceInline]
  322. errors = SongAdmin(Song, AdminSite()).check()
  323. expected = [
  324. checks.Error(
  325. "'ct_fk_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.",
  326. obj=InfluenceInline,
  327. id='admin.E303',
  328. )
  329. ]
  330. self.assertEqual(errors, expected)
  331. def test_generic_inline_model_admin_non_gfk_ct_field(self):
  332. """
  333. A GenericInlineModelAdmin raises problems if the ct_field points to a
  334. field that isn't part of a GenericForeignKey.
  335. """
  336. class InfluenceInline(GenericStackedInline):
  337. model = Influence
  338. ct_field = 'name'
  339. class SongAdmin(admin.ModelAdmin):
  340. inlines = [InfluenceInline]
  341. errors = SongAdmin(Song, AdminSite()).check()
  342. expected = [
  343. checks.Error(
  344. "'admin_checks.Influence' has no GenericForeignKey using "
  345. "content type field 'name' and object ID field 'object_id'.",
  346. obj=InfluenceInline,
  347. id='admin.E304',
  348. )
  349. ]
  350. self.assertEqual(errors, expected)
  351. def test_generic_inline_model_admin_non_gfk_fk_field(self):
  352. """
  353. A GenericInlineModelAdmin raises problems if the ct_fk_field points to
  354. a field that isn't part of a GenericForeignKey.
  355. """
  356. class InfluenceInline(GenericStackedInline):
  357. model = Influence
  358. ct_fk_field = 'name'
  359. class SongAdmin(admin.ModelAdmin):
  360. inlines = [InfluenceInline]
  361. errors = SongAdmin(Song, AdminSite()).check()
  362. expected = [
  363. checks.Error(
  364. "'admin_checks.Influence' has no GenericForeignKey using "
  365. "content type field 'content_type' and object ID field 'name'.",
  366. obj=InfluenceInline,
  367. id='admin.E304',
  368. )
  369. ]
  370. self.assertEqual(errors, expected)
  371. def test_app_label_in_admin_checks(self):
  372. """
  373. Regression test for #15669 - Include app label in admin system check messages
  374. """
  375. class RawIdNonexistingAdmin(admin.ModelAdmin):
  376. raw_id_fields = ('nonexisting',)
  377. errors = RawIdNonexistingAdmin(Album, AdminSite()).check()
  378. expected = [
  379. checks.Error(
  380. "The value of 'raw_id_fields[0]' refers to 'nonexisting', "
  381. "which is not an attribute of 'admin_checks.Album'.",
  382. obj=RawIdNonexistingAdmin,
  383. id='admin.E002',
  384. )
  385. ]
  386. self.assertEqual(errors, expected)
  387. def test_fk_exclusion(self):
  388. """
  389. Regression test for #11709 - when testing for fk excluding (when exclude is
  390. given) make sure fk_name is honored or things blow up when there is more
  391. than one fk to the parent model.
  392. """
  393. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  394. model = TwoAlbumFKAndAnE
  395. exclude = ("e",)
  396. fk_name = "album1"
  397. class MyAdmin(admin.ModelAdmin):
  398. inlines = [TwoAlbumFKAndAnEInline]
  399. errors = MyAdmin(Album, AdminSite()).check()
  400. self.assertEqual(errors, [])
  401. def test_inline_self_check(self):
  402. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  403. model = TwoAlbumFKAndAnE
  404. class MyAdmin(admin.ModelAdmin):
  405. inlines = [TwoAlbumFKAndAnEInline]
  406. errors = MyAdmin(Album, AdminSite()).check()
  407. expected = [
  408. checks.Error(
  409. "'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_checks.Album'.",
  410. obj=TwoAlbumFKAndAnEInline,
  411. id='admin.E202',
  412. )
  413. ]
  414. self.assertEqual(errors, expected)
  415. def test_inline_with_specified(self):
  416. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  417. model = TwoAlbumFKAndAnE
  418. fk_name = "album1"
  419. class MyAdmin(admin.ModelAdmin):
  420. inlines = [TwoAlbumFKAndAnEInline]
  421. errors = MyAdmin(Album, AdminSite()).check()
  422. self.assertEqual(errors, [])
  423. def test_readonly(self):
  424. class SongAdmin(admin.ModelAdmin):
  425. readonly_fields = ("title",)
  426. errors = SongAdmin(Song, AdminSite()).check()
  427. self.assertEqual(errors, [])
  428. def test_readonly_on_method(self):
  429. def my_function(obj):
  430. pass
  431. class SongAdmin(admin.ModelAdmin):
  432. readonly_fields = (my_function,)
  433. errors = SongAdmin(Song, AdminSite()).check()
  434. self.assertEqual(errors, [])
  435. def test_readonly_on_modeladmin(self):
  436. class SongAdmin(admin.ModelAdmin):
  437. readonly_fields = ("readonly_method_on_modeladmin",)
  438. def readonly_method_on_modeladmin(self, obj):
  439. pass
  440. errors = SongAdmin(Song, AdminSite()).check()
  441. self.assertEqual(errors, [])
  442. def test_readonly_dynamic_attribute_on_modeladmin(self):
  443. class SongAdmin(admin.ModelAdmin):
  444. readonly_fields = ("dynamic_method",)
  445. def __getattr__(self, item):
  446. if item == "dynamic_method":
  447. def method(obj):
  448. pass
  449. return method
  450. raise AttributeError
  451. errors = SongAdmin(Song, AdminSite()).check()
  452. self.assertEqual(errors, [])
  453. def test_readonly_method_on_model(self):
  454. class SongAdmin(admin.ModelAdmin):
  455. readonly_fields = ("readonly_method_on_model",)
  456. errors = SongAdmin(Song, AdminSite()).check()
  457. self.assertEqual(errors, [])
  458. def test_nonexistent_field(self):
  459. class SongAdmin(admin.ModelAdmin):
  460. readonly_fields = ("title", "nonexistent")
  461. errors = SongAdmin(Song, AdminSite()).check()
  462. expected = [
  463. checks.Error(
  464. "The value of 'readonly_fields[1]' is not a callable, an attribute "
  465. "of 'SongAdmin', or an attribute of 'admin_checks.Song'.",
  466. obj=SongAdmin,
  467. id='admin.E035',
  468. )
  469. ]
  470. self.assertEqual(errors, expected)
  471. def test_nonexistent_field_on_inline(self):
  472. class CityInline(admin.TabularInline):
  473. model = City
  474. readonly_fields = ['i_dont_exist'] # Missing attribute
  475. errors = CityInline(State, AdminSite()).check()
  476. expected = [
  477. checks.Error(
  478. "The value of 'readonly_fields[0]' is not a callable, an attribute "
  479. "of 'CityInline', or an attribute of 'admin_checks.City'.",
  480. obj=CityInline,
  481. id='admin.E035',
  482. )
  483. ]
  484. self.assertEqual(errors, expected)
  485. def test_extra(self):
  486. class SongAdmin(admin.ModelAdmin):
  487. def awesome_song(self, instance):
  488. if instance.title == "Born to Run":
  489. return "Best Ever!"
  490. return "Status unknown."
  491. errors = SongAdmin(Song, AdminSite()).check()
  492. self.assertEqual(errors, [])
  493. def test_readonly_lambda(self):
  494. class SongAdmin(admin.ModelAdmin):
  495. readonly_fields = (lambda obj: "test",)
  496. errors = SongAdmin(Song, AdminSite()).check()
  497. self.assertEqual(errors, [])
  498. def test_graceful_m2m_fail(self):
  499. """
  500. Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
  501. specifies the 'through' option is included in the 'fields' or the 'fieldsets'
  502. ModelAdmin options.
  503. """
  504. class BookAdmin(admin.ModelAdmin):
  505. fields = ['authors']
  506. errors = BookAdmin(Book, AdminSite()).check()
  507. expected = [
  508. checks.Error(
  509. "The value of 'fields' cannot include the ManyToManyField 'authors', "
  510. "because that field manually specifies a relationship model.",
  511. obj=BookAdmin,
  512. id='admin.E013',
  513. )
  514. ]
  515. self.assertEqual(errors, expected)
  516. def test_cannot_include_through(self):
  517. class FieldsetBookAdmin(admin.ModelAdmin):
  518. fieldsets = (
  519. ('Header 1', {'fields': ('name',)}),
  520. ('Header 2', {'fields': ('authors',)}),
  521. )
  522. errors = FieldsetBookAdmin(Book, AdminSite()).check()
  523. expected = [
  524. checks.Error(
  525. "The value of 'fieldsets[1][1][\"fields\"]' cannot include the ManyToManyField "
  526. "'authors', because that field manually specifies a relationship model.",
  527. obj=FieldsetBookAdmin,
  528. id='admin.E013',
  529. )
  530. ]
  531. self.assertEqual(errors, expected)
  532. def test_nested_fields(self):
  533. class NestedFieldsAdmin(admin.ModelAdmin):
  534. fields = ('price', ('name', 'subtitle'))
  535. errors = NestedFieldsAdmin(Book, AdminSite()).check()
  536. self.assertEqual(errors, [])
  537. def test_nested_fieldsets(self):
  538. class NestedFieldsetAdmin(admin.ModelAdmin):
  539. fieldsets = (
  540. ('Main', {'fields': ('price', ('name', 'subtitle'))}),
  541. )
  542. errors = NestedFieldsetAdmin(Book, AdminSite()).check()
  543. self.assertEqual(errors, [])
  544. def test_explicit_through_override(self):
  545. """
  546. Regression test for #12209 -- If the explicitly provided through model
  547. is specified as a string, the admin should still be able use
  548. Model.m2m_field.through
  549. """
  550. class AuthorsInline(admin.TabularInline):
  551. model = Book.authors.through
  552. class BookAdmin(admin.ModelAdmin):
  553. inlines = [AuthorsInline]
  554. errors = BookAdmin(Book, AdminSite()).check()
  555. self.assertEqual(errors, [])
  556. def test_non_model_fields(self):
  557. """
  558. Regression for ensuring ModelAdmin.fields can contain non-model fields
  559. that broke with r11737
  560. """
  561. class SongForm(forms.ModelForm):
  562. extra_data = forms.CharField()
  563. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  564. form = SongForm
  565. fields = ['title', 'extra_data']
  566. errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
  567. self.assertEqual(errors, [])
  568. def test_non_model_first_field(self):
  569. """
  570. Regression for ensuring ModelAdmin.field can handle first elem being a
  571. non-model field (test fix for UnboundLocalError introduced with r16225).
  572. """
  573. class SongForm(forms.ModelForm):
  574. extra_data = forms.CharField()
  575. class Meta:
  576. model = Song
  577. fields = '__all__'
  578. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  579. form = SongForm
  580. fields = ['extra_data', 'title']
  581. errors = FieldsOnFormOnlyAdmin(Song, AdminSite()).check()
  582. self.assertEqual(errors, [])
  583. def test_check_sublists_for_duplicates(self):
  584. class MyModelAdmin(admin.ModelAdmin):
  585. fields = ['state', ['state']]
  586. errors = MyModelAdmin(Song, AdminSite()).check()
  587. expected = [
  588. checks.Error(
  589. "The value of 'fields' contains duplicate field(s).",
  590. obj=MyModelAdmin,
  591. id='admin.E006'
  592. )
  593. ]
  594. self.assertEqual(errors, expected)
  595. def test_check_fieldset_sublists_for_duplicates(self):
  596. class MyModelAdmin(admin.ModelAdmin):
  597. fieldsets = [
  598. (None, {
  599. 'fields': ['title', 'album', ('title', 'album')]
  600. }),
  601. ]
  602. errors = MyModelAdmin(Song, AdminSite()).check()
  603. expected = [
  604. checks.Error(
  605. "There are duplicate field(s) in 'fieldsets[0][1]'.",
  606. obj=MyModelAdmin,
  607. id='admin.E012'
  608. )
  609. ]
  610. self.assertEqual(errors, expected)
  611. def test_list_filter_works_on_through_field_even_when_apps_not_ready(self):
  612. """
  613. Ensure list_filter can access reverse fields even when the app registry
  614. is not ready; refs #24146.
  615. """
  616. class BookAdminWithListFilter(admin.ModelAdmin):
  617. list_filter = ['authorsbooks__featured']
  618. # Temporarily pretending apps are not ready yet. This issue can happen
  619. # if the value of 'list_filter' refers to a 'through__field'.
  620. Book._meta.apps.ready = False
  621. try:
  622. errors = BookAdminWithListFilter(Book, AdminSite()).check()
  623. self.assertEqual(errors, [])
  624. finally:
  625. Book._meta.apps.ready = True