test_checks.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.admin import BooleanFieldListFilter, SimpleListFilter
  4. from django.contrib.admin.options import VERTICAL, ModelAdmin, TabularInline
  5. from django.contrib.admin.sites import AdminSite
  6. from django.core.checks import Error
  7. from django.db.models import CASCADE, F, Field, ForeignKey, Model
  8. from django.db.models.functions import Upper
  9. from django.forms.models import BaseModelFormSet
  10. from django.test import SimpleTestCase
  11. from .models import Band, Song, User, ValidationTestInlineModel, ValidationTestModel
  12. class CheckTestCase(SimpleTestCase):
  13. def assertIsInvalid(
  14. self,
  15. model_admin,
  16. model,
  17. msg,
  18. id=None,
  19. hint=None,
  20. invalid_obj=None,
  21. admin_site=None,
  22. ):
  23. if admin_site is None:
  24. admin_site = AdminSite()
  25. invalid_obj = invalid_obj or model_admin
  26. admin_obj = model_admin(model, admin_site)
  27. self.assertEqual(
  28. admin_obj.check(), [Error(msg, hint=hint, obj=invalid_obj, id=id)]
  29. )
  30. def assertIsInvalidRegexp(
  31. self, model_admin, model, msg, id=None, hint=None, invalid_obj=None
  32. ):
  33. """
  34. Same as assertIsInvalid but treats the given msg as a regexp.
  35. """
  36. invalid_obj = invalid_obj or model_admin
  37. admin_obj = model_admin(model, AdminSite())
  38. errors = admin_obj.check()
  39. self.assertEqual(len(errors), 1)
  40. error = errors[0]
  41. self.assertEqual(error.hint, hint)
  42. self.assertEqual(error.obj, invalid_obj)
  43. self.assertEqual(error.id, id)
  44. self.assertRegex(error.msg, msg)
  45. def assertIsValid(self, model_admin, model, admin_site=None):
  46. if admin_site is None:
  47. admin_site = AdminSite()
  48. admin_obj = model_admin(model, admin_site)
  49. self.assertEqual(admin_obj.check(), [])
  50. class RawIdCheckTests(CheckTestCase):
  51. def test_not_iterable(self):
  52. class TestModelAdmin(ModelAdmin):
  53. raw_id_fields = 10
  54. self.assertIsInvalid(
  55. TestModelAdmin,
  56. ValidationTestModel,
  57. "The value of 'raw_id_fields' must be a list or tuple.",
  58. "admin.E001",
  59. )
  60. def test_missing_field(self):
  61. class TestModelAdmin(ModelAdmin):
  62. raw_id_fields = ("non_existent_field",)
  63. self.assertIsInvalid(
  64. TestModelAdmin,
  65. ValidationTestModel,
  66. "The value of 'raw_id_fields[0]' refers to 'non_existent_field', "
  67. "which is not a field of 'modeladmin.ValidationTestModel'.",
  68. "admin.E002",
  69. )
  70. def test_invalid_field_type(self):
  71. class TestModelAdmin(ModelAdmin):
  72. raw_id_fields = ("name",)
  73. self.assertIsInvalid(
  74. TestModelAdmin,
  75. ValidationTestModel,
  76. "The value of 'raw_id_fields[0]' must be a foreign key or a "
  77. "many-to-many field.",
  78. "admin.E003",
  79. )
  80. def test_valid_case(self):
  81. class TestModelAdmin(ModelAdmin):
  82. raw_id_fields = ("users",)
  83. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  84. def test_field_attname(self):
  85. class TestModelAdmin(ModelAdmin):
  86. raw_id_fields = ["band_id"]
  87. self.assertIsInvalid(
  88. TestModelAdmin,
  89. ValidationTestModel,
  90. "The value of 'raw_id_fields[0]' refers to 'band_id', which is "
  91. "not a field of 'modeladmin.ValidationTestModel'.",
  92. "admin.E002",
  93. )
  94. class FieldsetsCheckTests(CheckTestCase):
  95. def test_valid_case(self):
  96. class TestModelAdmin(ModelAdmin):
  97. fieldsets = (("General", {"fields": ("name",)}),)
  98. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  99. def test_not_iterable(self):
  100. class TestModelAdmin(ModelAdmin):
  101. fieldsets = 10
  102. self.assertIsInvalid(
  103. TestModelAdmin,
  104. ValidationTestModel,
  105. "The value of 'fieldsets' must be a list or tuple.",
  106. "admin.E007",
  107. )
  108. def test_non_iterable_item(self):
  109. class TestModelAdmin(ModelAdmin):
  110. fieldsets = ({},)
  111. self.assertIsInvalid(
  112. TestModelAdmin,
  113. ValidationTestModel,
  114. "The value of 'fieldsets[0]' must be a list or tuple.",
  115. "admin.E008",
  116. )
  117. def test_item_not_a_pair(self):
  118. class TestModelAdmin(ModelAdmin):
  119. fieldsets = ((),)
  120. self.assertIsInvalid(
  121. TestModelAdmin,
  122. ValidationTestModel,
  123. "The value of 'fieldsets[0]' must be of length 2.",
  124. "admin.E009",
  125. )
  126. def test_second_element_of_item_not_a_dict(self):
  127. class TestModelAdmin(ModelAdmin):
  128. fieldsets = (("General", ()),)
  129. self.assertIsInvalid(
  130. TestModelAdmin,
  131. ValidationTestModel,
  132. "The value of 'fieldsets[0][1]' must be a dictionary.",
  133. "admin.E010",
  134. )
  135. def test_missing_fields_key(self):
  136. class TestModelAdmin(ModelAdmin):
  137. fieldsets = (("General", {}),)
  138. self.assertIsInvalid(
  139. TestModelAdmin,
  140. ValidationTestModel,
  141. "The value of 'fieldsets[0][1]' must contain the key 'fields'.",
  142. "admin.E011",
  143. )
  144. class TestModelAdmin(ModelAdmin):
  145. fieldsets = (("General", {"fields": ("name",)}),)
  146. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  147. def test_specified_both_fields_and_fieldsets(self):
  148. class TestModelAdmin(ModelAdmin):
  149. fieldsets = (("General", {"fields": ("name",)}),)
  150. fields = ["name"]
  151. self.assertIsInvalid(
  152. TestModelAdmin,
  153. ValidationTestModel,
  154. "Both 'fieldsets' and 'fields' are specified.",
  155. "admin.E005",
  156. )
  157. def test_duplicate_fields(self):
  158. class TestModelAdmin(ModelAdmin):
  159. fieldsets = [(None, {"fields": ["name", "name"]})]
  160. self.assertIsInvalid(
  161. TestModelAdmin,
  162. ValidationTestModel,
  163. "There are duplicate field(s) in 'fieldsets[0][1]'.",
  164. "admin.E012",
  165. )
  166. def test_duplicate_fields_in_fieldsets(self):
  167. class TestModelAdmin(ModelAdmin):
  168. fieldsets = [
  169. (None, {"fields": ["name"]}),
  170. (None, {"fields": ["name"]}),
  171. ]
  172. self.assertIsInvalid(
  173. TestModelAdmin,
  174. ValidationTestModel,
  175. "There are duplicate field(s) in 'fieldsets[1][1]'.",
  176. "admin.E012",
  177. )
  178. def test_fieldsets_with_custom_form_validation(self):
  179. class BandAdmin(ModelAdmin):
  180. fieldsets = (("Band", {"fields": ("name",)}),)
  181. self.assertIsValid(BandAdmin, Band)
  182. class FieldsCheckTests(CheckTestCase):
  183. def test_duplicate_fields_in_fields(self):
  184. class TestModelAdmin(ModelAdmin):
  185. fields = ["name", "name"]
  186. self.assertIsInvalid(
  187. TestModelAdmin,
  188. ValidationTestModel,
  189. "The value of 'fields' contains duplicate field(s).",
  190. "admin.E006",
  191. )
  192. def test_inline(self):
  193. class ValidationTestInline(TabularInline):
  194. model = ValidationTestInlineModel
  195. fields = 10
  196. class TestModelAdmin(ModelAdmin):
  197. inlines = [ValidationTestInline]
  198. self.assertIsInvalid(
  199. TestModelAdmin,
  200. ValidationTestModel,
  201. "The value of 'fields' must be a list or tuple.",
  202. "admin.E004",
  203. invalid_obj=ValidationTestInline,
  204. )
  205. class FormCheckTests(CheckTestCase):
  206. def test_invalid_type(self):
  207. class FakeForm:
  208. pass
  209. class TestModelAdmin(ModelAdmin):
  210. form = FakeForm
  211. class TestModelAdminWithNoForm(ModelAdmin):
  212. form = "not a form"
  213. for model_admin in (TestModelAdmin, TestModelAdminWithNoForm):
  214. with self.subTest(model_admin):
  215. self.assertIsInvalid(
  216. model_admin,
  217. ValidationTestModel,
  218. "The value of 'form' must inherit from 'BaseModelForm'.",
  219. "admin.E016",
  220. )
  221. def test_fieldsets_with_custom_form_validation(self):
  222. class BandAdmin(ModelAdmin):
  223. fieldsets = (("Band", {"fields": ("name",)}),)
  224. self.assertIsValid(BandAdmin, Band)
  225. def test_valid_case(self):
  226. class AdminBandForm(forms.ModelForm):
  227. delete = forms.BooleanField()
  228. class BandAdmin(ModelAdmin):
  229. form = AdminBandForm
  230. fieldsets = (("Band", {"fields": ("name", "bio", "sign_date", "delete")}),)
  231. self.assertIsValid(BandAdmin, Band)
  232. class FilterVerticalCheckTests(CheckTestCase):
  233. def test_not_iterable(self):
  234. class TestModelAdmin(ModelAdmin):
  235. filter_vertical = 10
  236. self.assertIsInvalid(
  237. TestModelAdmin,
  238. ValidationTestModel,
  239. "The value of 'filter_vertical' must be a list or tuple.",
  240. "admin.E017",
  241. )
  242. def test_missing_field(self):
  243. class TestModelAdmin(ModelAdmin):
  244. filter_vertical = ("non_existent_field",)
  245. self.assertIsInvalid(
  246. TestModelAdmin,
  247. ValidationTestModel,
  248. "The value of 'filter_vertical[0]' refers to 'non_existent_field', "
  249. "which is not a field of 'modeladmin.ValidationTestModel'.",
  250. "admin.E019",
  251. )
  252. def test_invalid_field_type(self):
  253. class TestModelAdmin(ModelAdmin):
  254. filter_vertical = ("name",)
  255. self.assertIsInvalid(
  256. TestModelAdmin,
  257. ValidationTestModel,
  258. "The value of 'filter_vertical[0]' must be a many-to-many field.",
  259. "admin.E020",
  260. )
  261. def test_valid_case(self):
  262. class TestModelAdmin(ModelAdmin):
  263. filter_vertical = ("users",)
  264. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  265. class FilterHorizontalCheckTests(CheckTestCase):
  266. def test_not_iterable(self):
  267. class TestModelAdmin(ModelAdmin):
  268. filter_horizontal = 10
  269. self.assertIsInvalid(
  270. TestModelAdmin,
  271. ValidationTestModel,
  272. "The value of 'filter_horizontal' must be a list or tuple.",
  273. "admin.E018",
  274. )
  275. def test_missing_field(self):
  276. class TestModelAdmin(ModelAdmin):
  277. filter_horizontal = ("non_existent_field",)
  278. self.assertIsInvalid(
  279. TestModelAdmin,
  280. ValidationTestModel,
  281. "The value of 'filter_horizontal[0]' refers to 'non_existent_field', "
  282. "which is not a field of 'modeladmin.ValidationTestModel'.",
  283. "admin.E019",
  284. )
  285. def test_invalid_field_type(self):
  286. class TestModelAdmin(ModelAdmin):
  287. filter_horizontal = ("name",)
  288. self.assertIsInvalid(
  289. TestModelAdmin,
  290. ValidationTestModel,
  291. "The value of 'filter_horizontal[0]' must be a many-to-many field.",
  292. "admin.E020",
  293. )
  294. def test_valid_case(self):
  295. class TestModelAdmin(ModelAdmin):
  296. filter_horizontal = ("users",)
  297. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  298. class RadioFieldsCheckTests(CheckTestCase):
  299. def test_not_dictionary(self):
  300. class TestModelAdmin(ModelAdmin):
  301. radio_fields = ()
  302. self.assertIsInvalid(
  303. TestModelAdmin,
  304. ValidationTestModel,
  305. "The value of 'radio_fields' must be a dictionary.",
  306. "admin.E021",
  307. )
  308. def test_missing_field(self):
  309. class TestModelAdmin(ModelAdmin):
  310. radio_fields = {"non_existent_field": VERTICAL}
  311. self.assertIsInvalid(
  312. TestModelAdmin,
  313. ValidationTestModel,
  314. "The value of 'radio_fields' refers to 'non_existent_field', "
  315. "which is not a field of 'modeladmin.ValidationTestModel'.",
  316. "admin.E022",
  317. )
  318. def test_invalid_field_type(self):
  319. class TestModelAdmin(ModelAdmin):
  320. radio_fields = {"name": VERTICAL}
  321. self.assertIsInvalid(
  322. TestModelAdmin,
  323. ValidationTestModel,
  324. "The value of 'radio_fields' refers to 'name', which is not an instance "
  325. "of ForeignKey, and does not have a 'choices' definition.",
  326. "admin.E023",
  327. )
  328. def test_invalid_value(self):
  329. class TestModelAdmin(ModelAdmin):
  330. radio_fields = {"state": None}
  331. self.assertIsInvalid(
  332. TestModelAdmin,
  333. ValidationTestModel,
  334. "The value of 'radio_fields[\"state\"]' must be either admin.HORIZONTAL or "
  335. "admin.VERTICAL.",
  336. "admin.E024",
  337. )
  338. def test_valid_case(self):
  339. class TestModelAdmin(ModelAdmin):
  340. radio_fields = {"state": VERTICAL}
  341. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  342. class PrepopulatedFieldsCheckTests(CheckTestCase):
  343. def test_not_list_or_tuple(self):
  344. class TestModelAdmin(ModelAdmin):
  345. prepopulated_fields = {"slug": "test"}
  346. self.assertIsInvalid(
  347. TestModelAdmin,
  348. ValidationTestModel,
  349. "The value of 'prepopulated_fields[\"slug\"]' must be a list or tuple.",
  350. "admin.E029",
  351. )
  352. def test_not_dictionary(self):
  353. class TestModelAdmin(ModelAdmin):
  354. prepopulated_fields = ()
  355. self.assertIsInvalid(
  356. TestModelAdmin,
  357. ValidationTestModel,
  358. "The value of 'prepopulated_fields' must be a dictionary.",
  359. "admin.E026",
  360. )
  361. def test_missing_field(self):
  362. class TestModelAdmin(ModelAdmin):
  363. prepopulated_fields = {"non_existent_field": ("slug",)}
  364. self.assertIsInvalid(
  365. TestModelAdmin,
  366. ValidationTestModel,
  367. "The value of 'prepopulated_fields' refers to 'non_existent_field', "
  368. "which is not a field of 'modeladmin.ValidationTestModel'.",
  369. "admin.E027",
  370. )
  371. def test_missing_field_again(self):
  372. class TestModelAdmin(ModelAdmin):
  373. prepopulated_fields = {"slug": ("non_existent_field",)}
  374. self.assertIsInvalid(
  375. TestModelAdmin,
  376. ValidationTestModel,
  377. "The value of 'prepopulated_fields[\"slug\"][0]' refers to "
  378. "'non_existent_field', which is not a field of "
  379. "'modeladmin.ValidationTestModel'.",
  380. "admin.E030",
  381. )
  382. def test_invalid_field_type(self):
  383. class TestModelAdmin(ModelAdmin):
  384. prepopulated_fields = {"users": ("name",)}
  385. self.assertIsInvalid(
  386. TestModelAdmin,
  387. ValidationTestModel,
  388. "The value of 'prepopulated_fields' refers to 'users', which must not be "
  389. "a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.",
  390. "admin.E028",
  391. )
  392. def test_valid_case(self):
  393. class TestModelAdmin(ModelAdmin):
  394. prepopulated_fields = {"slug": ("name",)}
  395. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  396. def test_one_to_one_field(self):
  397. class TestModelAdmin(ModelAdmin):
  398. prepopulated_fields = {"best_friend": ("name",)}
  399. self.assertIsInvalid(
  400. TestModelAdmin,
  401. ValidationTestModel,
  402. "The value of 'prepopulated_fields' refers to 'best_friend', which must "
  403. "not be a DateTimeField, a ForeignKey, a OneToOneField, or a "
  404. "ManyToManyField.",
  405. "admin.E028",
  406. )
  407. class ListDisplayTests(CheckTestCase):
  408. def test_not_iterable(self):
  409. class TestModelAdmin(ModelAdmin):
  410. list_display = 10
  411. self.assertIsInvalid(
  412. TestModelAdmin,
  413. ValidationTestModel,
  414. "The value of 'list_display' must be a list or tuple.",
  415. "admin.E107",
  416. )
  417. def test_missing_field(self):
  418. class TestModelAdmin(ModelAdmin):
  419. list_display = ("non_existent_field",)
  420. self.assertIsInvalid(
  421. TestModelAdmin,
  422. ValidationTestModel,
  423. "The value of 'list_display[0]' refers to 'non_existent_field', "
  424. "which is not a callable, an attribute of 'TestModelAdmin', "
  425. "or an attribute or method on 'modeladmin.ValidationTestModel'.",
  426. "admin.E108",
  427. )
  428. def test_invalid_field_type(self):
  429. class TestModelAdmin(ModelAdmin):
  430. list_display = ("users",)
  431. self.assertIsInvalid(
  432. TestModelAdmin,
  433. ValidationTestModel,
  434. "The value of 'list_display[0]' must not be a many-to-many field or a "
  435. "reverse foreign key.",
  436. "admin.E109",
  437. )
  438. def test_invalid_reverse_related_field(self):
  439. class TestModelAdmin(ModelAdmin):
  440. list_display = ["song_set"]
  441. self.assertIsInvalid(
  442. TestModelAdmin,
  443. Band,
  444. "The value of 'list_display[0]' must not be a many-to-many field or a "
  445. "reverse foreign key.",
  446. "admin.E109",
  447. )
  448. def test_valid_case(self):
  449. @admin.display
  450. def a_callable(obj):
  451. pass
  452. class TestModelAdmin(ModelAdmin):
  453. @admin.display
  454. def a_method(self, obj):
  455. pass
  456. list_display = ("name", "decade_published_in", "a_method", a_callable)
  457. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  458. def test_valid_field_accessible_via_instance(self):
  459. class PositionField(Field):
  460. """Custom field accessible only via instance."""
  461. def contribute_to_class(self, cls, name):
  462. super().contribute_to_class(cls, name)
  463. setattr(cls, self.name, self)
  464. def __get__(self, instance, owner):
  465. if instance is None:
  466. raise AttributeError()
  467. class TestModel(Model):
  468. field = PositionField()
  469. class TestModelAdmin(ModelAdmin):
  470. list_display = ("field",)
  471. self.assertIsValid(TestModelAdmin, TestModel)
  472. class ListDisplayLinksCheckTests(CheckTestCase):
  473. def test_not_iterable(self):
  474. class TestModelAdmin(ModelAdmin):
  475. list_display_links = 10
  476. self.assertIsInvalid(
  477. TestModelAdmin,
  478. ValidationTestModel,
  479. "The value of 'list_display_links' must be a list, a tuple, or None.",
  480. "admin.E110",
  481. )
  482. def test_missing_field(self):
  483. class TestModelAdmin(ModelAdmin):
  484. list_display_links = ("non_existent_field",)
  485. self.assertIsInvalid(
  486. TestModelAdmin,
  487. ValidationTestModel,
  488. (
  489. "The value of 'list_display_links[0]' refers to "
  490. "'non_existent_field', which is not defined in 'list_display'."
  491. ),
  492. "admin.E111",
  493. )
  494. def test_missing_in_list_display(self):
  495. class TestModelAdmin(ModelAdmin):
  496. list_display_links = ("name",)
  497. self.assertIsInvalid(
  498. TestModelAdmin,
  499. ValidationTestModel,
  500. "The value of 'list_display_links[0]' refers to 'name', which is not "
  501. "defined in 'list_display'.",
  502. "admin.E111",
  503. )
  504. def test_valid_case(self):
  505. @admin.display
  506. def a_callable(obj):
  507. pass
  508. class TestModelAdmin(ModelAdmin):
  509. @admin.display
  510. def a_method(self, obj):
  511. pass
  512. list_display = ("name", "decade_published_in", "a_method", a_callable)
  513. list_display_links = ("name", "decade_published_in", "a_method", a_callable)
  514. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  515. def test_None_is_valid_case(self):
  516. class TestModelAdmin(ModelAdmin):
  517. list_display_links = None
  518. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  519. def test_list_display_links_check_skipped_if_get_list_display_overridden(self):
  520. """
  521. list_display_links check is skipped if get_list_display() is overridden.
  522. """
  523. class TestModelAdmin(ModelAdmin):
  524. list_display_links = ["name", "subtitle"]
  525. def get_list_display(self, request):
  526. pass
  527. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  528. def test_list_display_link_checked_for_list_tuple_if_get_list_display_overridden(
  529. self,
  530. ):
  531. """
  532. list_display_links is checked for list/tuple/None even if
  533. get_list_display() is overridden.
  534. """
  535. class TestModelAdmin(ModelAdmin):
  536. list_display_links = "non-list/tuple"
  537. def get_list_display(self, request):
  538. pass
  539. self.assertIsInvalid(
  540. TestModelAdmin,
  541. ValidationTestModel,
  542. "The value of 'list_display_links' must be a list, a tuple, or None.",
  543. "admin.E110",
  544. )
  545. class ListFilterTests(CheckTestCase):
  546. def test_list_filter_validation(self):
  547. class TestModelAdmin(ModelAdmin):
  548. list_filter = 10
  549. self.assertIsInvalid(
  550. TestModelAdmin,
  551. ValidationTestModel,
  552. "The value of 'list_filter' must be a list or tuple.",
  553. "admin.E112",
  554. )
  555. def test_not_list_filter_class(self):
  556. class TestModelAdmin(ModelAdmin):
  557. list_filter = ["RandomClass"]
  558. self.assertIsInvalid(
  559. TestModelAdmin,
  560. ValidationTestModel,
  561. "The value of 'list_filter[0]' refers to 'RandomClass', which "
  562. "does not refer to a Field.",
  563. "admin.E116",
  564. )
  565. def test_callable(self):
  566. def random_callable():
  567. pass
  568. class TestModelAdmin(ModelAdmin):
  569. list_filter = [random_callable]
  570. self.assertIsInvalid(
  571. TestModelAdmin,
  572. ValidationTestModel,
  573. "The value of 'list_filter[0]' must inherit from 'ListFilter'.",
  574. "admin.E113",
  575. )
  576. def test_not_callable(self):
  577. class TestModelAdmin(ModelAdmin):
  578. list_filter = [[42, 42]]
  579. self.assertIsInvalid(
  580. TestModelAdmin,
  581. ValidationTestModel,
  582. "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
  583. "admin.E115",
  584. )
  585. def test_missing_field(self):
  586. class TestModelAdmin(ModelAdmin):
  587. list_filter = ("non_existent_field",)
  588. self.assertIsInvalid(
  589. TestModelAdmin,
  590. ValidationTestModel,
  591. "The value of 'list_filter[0]' refers to 'non_existent_field', "
  592. "which does not refer to a Field.",
  593. "admin.E116",
  594. )
  595. def test_not_filter(self):
  596. class RandomClass:
  597. pass
  598. class TestModelAdmin(ModelAdmin):
  599. list_filter = (RandomClass,)
  600. self.assertIsInvalid(
  601. TestModelAdmin,
  602. ValidationTestModel,
  603. "The value of 'list_filter[0]' must inherit from 'ListFilter'.",
  604. "admin.E113",
  605. )
  606. def test_not_filter_again(self):
  607. class RandomClass:
  608. pass
  609. class TestModelAdmin(ModelAdmin):
  610. list_filter = (("is_active", RandomClass),)
  611. self.assertIsInvalid(
  612. TestModelAdmin,
  613. ValidationTestModel,
  614. "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
  615. "admin.E115",
  616. )
  617. def test_not_filter_again_again(self):
  618. class AwesomeFilter(SimpleListFilter):
  619. def get_title(self):
  620. return "awesomeness"
  621. def get_choices(self, request):
  622. return (("bit", "A bit awesome"), ("very", "Very awesome"))
  623. def get_queryset(self, cl, qs):
  624. return qs
  625. class TestModelAdmin(ModelAdmin):
  626. list_filter = (("is_active", AwesomeFilter),)
  627. self.assertIsInvalid(
  628. TestModelAdmin,
  629. ValidationTestModel,
  630. "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
  631. "admin.E115",
  632. )
  633. def test_list_filter_is_func(self):
  634. def get_filter():
  635. pass
  636. class TestModelAdmin(ModelAdmin):
  637. list_filter = [get_filter]
  638. self.assertIsInvalid(
  639. TestModelAdmin,
  640. ValidationTestModel,
  641. "The value of 'list_filter[0]' must inherit from 'ListFilter'.",
  642. "admin.E113",
  643. )
  644. def test_not_associated_with_field_name(self):
  645. class TestModelAdmin(ModelAdmin):
  646. list_filter = (BooleanFieldListFilter,)
  647. self.assertIsInvalid(
  648. TestModelAdmin,
  649. ValidationTestModel,
  650. "The value of 'list_filter[0]' must not inherit from 'FieldListFilter'.",
  651. "admin.E114",
  652. )
  653. def test_valid_case(self):
  654. class AwesomeFilter(SimpleListFilter):
  655. def get_title(self):
  656. return "awesomeness"
  657. def get_choices(self, request):
  658. return (("bit", "A bit awesome"), ("very", "Very awesome"))
  659. def get_queryset(self, cl, qs):
  660. return qs
  661. class TestModelAdmin(ModelAdmin):
  662. list_filter = (
  663. "is_active",
  664. AwesomeFilter,
  665. ("is_active", BooleanFieldListFilter),
  666. "no",
  667. )
  668. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  669. class ListPerPageCheckTests(CheckTestCase):
  670. def test_not_integer(self):
  671. class TestModelAdmin(ModelAdmin):
  672. list_per_page = "hello"
  673. self.assertIsInvalid(
  674. TestModelAdmin,
  675. ValidationTestModel,
  676. "The value of 'list_per_page' must be an integer.",
  677. "admin.E118",
  678. )
  679. def test_valid_case(self):
  680. class TestModelAdmin(ModelAdmin):
  681. list_per_page = 100
  682. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  683. class ListMaxShowAllCheckTests(CheckTestCase):
  684. def test_not_integer(self):
  685. class TestModelAdmin(ModelAdmin):
  686. list_max_show_all = "hello"
  687. self.assertIsInvalid(
  688. TestModelAdmin,
  689. ValidationTestModel,
  690. "The value of 'list_max_show_all' must be an integer.",
  691. "admin.E119",
  692. )
  693. def test_valid_case(self):
  694. class TestModelAdmin(ModelAdmin):
  695. list_max_show_all = 200
  696. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  697. class SearchFieldsCheckTests(CheckTestCase):
  698. def test_not_iterable(self):
  699. class TestModelAdmin(ModelAdmin):
  700. search_fields = 10
  701. self.assertIsInvalid(
  702. TestModelAdmin,
  703. ValidationTestModel,
  704. "The value of 'search_fields' must be a list or tuple.",
  705. "admin.E126",
  706. )
  707. class DateHierarchyCheckTests(CheckTestCase):
  708. def test_missing_field(self):
  709. class TestModelAdmin(ModelAdmin):
  710. date_hierarchy = "non_existent_field"
  711. self.assertIsInvalid(
  712. TestModelAdmin,
  713. ValidationTestModel,
  714. "The value of 'date_hierarchy' refers to 'non_existent_field', "
  715. "which does not refer to a Field.",
  716. "admin.E127",
  717. )
  718. def test_invalid_field_type(self):
  719. class TestModelAdmin(ModelAdmin):
  720. date_hierarchy = "name"
  721. self.assertIsInvalid(
  722. TestModelAdmin,
  723. ValidationTestModel,
  724. "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
  725. "admin.E128",
  726. )
  727. def test_valid_case(self):
  728. class TestModelAdmin(ModelAdmin):
  729. date_hierarchy = "pub_date"
  730. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  731. def test_related_valid_case(self):
  732. class TestModelAdmin(ModelAdmin):
  733. date_hierarchy = "band__sign_date"
  734. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  735. def test_related_invalid_field_type(self):
  736. class TestModelAdmin(ModelAdmin):
  737. date_hierarchy = "band__name"
  738. self.assertIsInvalid(
  739. TestModelAdmin,
  740. ValidationTestModel,
  741. "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
  742. "admin.E128",
  743. )
  744. class OrderingCheckTests(CheckTestCase):
  745. def test_not_iterable(self):
  746. class TestModelAdmin(ModelAdmin):
  747. ordering = 10
  748. self.assertIsInvalid(
  749. TestModelAdmin,
  750. ValidationTestModel,
  751. "The value of 'ordering' must be a list or tuple.",
  752. "admin.E031",
  753. )
  754. class TestModelAdmin(ModelAdmin):
  755. ordering = ("non_existent_field",)
  756. self.assertIsInvalid(
  757. TestModelAdmin,
  758. ValidationTestModel,
  759. "The value of 'ordering[0]' refers to 'non_existent_field', "
  760. "which is not a field of 'modeladmin.ValidationTestModel'.",
  761. "admin.E033",
  762. )
  763. def test_random_marker_not_alone(self):
  764. class TestModelAdmin(ModelAdmin):
  765. ordering = ("?", "name")
  766. self.assertIsInvalid(
  767. TestModelAdmin,
  768. ValidationTestModel,
  769. "The value of 'ordering' has the random ordering marker '?', but contains "
  770. "other fields as well.",
  771. "admin.E032",
  772. hint='Either remove the "?", or remove the other fields.',
  773. )
  774. def test_valid_random_marker_case(self):
  775. class TestModelAdmin(ModelAdmin):
  776. ordering = ("?",)
  777. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  778. def test_valid_complex_case(self):
  779. class TestModelAdmin(ModelAdmin):
  780. ordering = ("band__name",)
  781. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  782. def test_valid_case(self):
  783. class TestModelAdmin(ModelAdmin):
  784. ordering = ("name", "pk")
  785. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  786. def test_invalid_expression(self):
  787. class TestModelAdmin(ModelAdmin):
  788. ordering = (F("nonexistent"),)
  789. self.assertIsInvalid(
  790. TestModelAdmin,
  791. ValidationTestModel,
  792. "The value of 'ordering[0]' refers to 'nonexistent', which is not "
  793. "a field of 'modeladmin.ValidationTestModel'.",
  794. "admin.E033",
  795. )
  796. def test_valid_expression(self):
  797. class TestModelAdmin(ModelAdmin):
  798. ordering = (Upper("name"), Upper("band__name").desc())
  799. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  800. class ListSelectRelatedCheckTests(CheckTestCase):
  801. def test_invalid_type(self):
  802. class TestModelAdmin(ModelAdmin):
  803. list_select_related = 1
  804. self.assertIsInvalid(
  805. TestModelAdmin,
  806. ValidationTestModel,
  807. "The value of 'list_select_related' must be a boolean, tuple or list.",
  808. "admin.E117",
  809. )
  810. def test_valid_case(self):
  811. class TestModelAdmin(ModelAdmin):
  812. list_select_related = False
  813. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  814. class SaveAsCheckTests(CheckTestCase):
  815. def test_not_boolean(self):
  816. class TestModelAdmin(ModelAdmin):
  817. save_as = 1
  818. self.assertIsInvalid(
  819. TestModelAdmin,
  820. ValidationTestModel,
  821. "The value of 'save_as' must be a boolean.",
  822. "admin.E101",
  823. )
  824. def test_valid_case(self):
  825. class TestModelAdmin(ModelAdmin):
  826. save_as = True
  827. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  828. class SaveOnTopCheckTests(CheckTestCase):
  829. def test_not_boolean(self):
  830. class TestModelAdmin(ModelAdmin):
  831. save_on_top = 1
  832. self.assertIsInvalid(
  833. TestModelAdmin,
  834. ValidationTestModel,
  835. "The value of 'save_on_top' must be a boolean.",
  836. "admin.E102",
  837. )
  838. def test_valid_case(self):
  839. class TestModelAdmin(ModelAdmin):
  840. save_on_top = True
  841. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  842. class InlinesCheckTests(CheckTestCase):
  843. def test_not_iterable(self):
  844. class TestModelAdmin(ModelAdmin):
  845. inlines = 10
  846. self.assertIsInvalid(
  847. TestModelAdmin,
  848. ValidationTestModel,
  849. "The value of 'inlines' must be a list or tuple.",
  850. "admin.E103",
  851. )
  852. def test_not_correct_inline_field(self):
  853. class TestModelAdmin(ModelAdmin):
  854. inlines = [42]
  855. self.assertIsInvalidRegexp(
  856. TestModelAdmin,
  857. ValidationTestModel,
  858. r"'.*\.TestModelAdmin' must inherit from 'InlineModelAdmin'\.",
  859. "admin.E104",
  860. )
  861. def test_not_model_admin(self):
  862. class ValidationTestInline:
  863. pass
  864. class TestModelAdmin(ModelAdmin):
  865. inlines = [ValidationTestInline]
  866. self.assertIsInvalidRegexp(
  867. TestModelAdmin,
  868. ValidationTestModel,
  869. r"'.*\.ValidationTestInline' must inherit from 'InlineModelAdmin'\.",
  870. "admin.E104",
  871. )
  872. def test_missing_model_field(self):
  873. class ValidationTestInline(TabularInline):
  874. pass
  875. class TestModelAdmin(ModelAdmin):
  876. inlines = [ValidationTestInline]
  877. self.assertIsInvalidRegexp(
  878. TestModelAdmin,
  879. ValidationTestModel,
  880. r"'.*\.ValidationTestInline' must have a 'model' attribute\.",
  881. "admin.E105",
  882. )
  883. def test_invalid_model_type(self):
  884. class SomethingBad:
  885. pass
  886. class ValidationTestInline(TabularInline):
  887. model = SomethingBad
  888. class TestModelAdmin(ModelAdmin):
  889. inlines = [ValidationTestInline]
  890. self.assertIsInvalidRegexp(
  891. TestModelAdmin,
  892. ValidationTestModel,
  893. r"The value of '.*\.ValidationTestInline.model' must be a Model\.",
  894. "admin.E106",
  895. )
  896. def test_invalid_model(self):
  897. class ValidationTestInline(TabularInline):
  898. model = "Not a class"
  899. class TestModelAdmin(ModelAdmin):
  900. inlines = [ValidationTestInline]
  901. self.assertIsInvalidRegexp(
  902. TestModelAdmin,
  903. ValidationTestModel,
  904. r"The value of '.*\.ValidationTestInline.model' must be a Model\.",
  905. "admin.E106",
  906. )
  907. def test_invalid_callable(self):
  908. def random_obj():
  909. pass
  910. class TestModelAdmin(ModelAdmin):
  911. inlines = [random_obj]
  912. self.assertIsInvalidRegexp(
  913. TestModelAdmin,
  914. ValidationTestModel,
  915. r"'.*\.random_obj' must inherit from 'InlineModelAdmin'\.",
  916. "admin.E104",
  917. )
  918. def test_valid_case(self):
  919. class ValidationTestInline(TabularInline):
  920. model = ValidationTestInlineModel
  921. class TestModelAdmin(ModelAdmin):
  922. inlines = [ValidationTestInline]
  923. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  924. class FkNameCheckTests(CheckTestCase):
  925. def test_missing_field(self):
  926. class ValidationTestInline(TabularInline):
  927. model = ValidationTestInlineModel
  928. fk_name = "non_existent_field"
  929. class TestModelAdmin(ModelAdmin):
  930. inlines = [ValidationTestInline]
  931. self.assertIsInvalid(
  932. TestModelAdmin,
  933. ValidationTestModel,
  934. "'modeladmin.ValidationTestInlineModel' has no field named "
  935. "'non_existent_field'.",
  936. "admin.E202",
  937. invalid_obj=ValidationTestInline,
  938. )
  939. def test_valid_case(self):
  940. class ValidationTestInline(TabularInline):
  941. model = ValidationTestInlineModel
  942. fk_name = "parent"
  943. class TestModelAdmin(ModelAdmin):
  944. inlines = [ValidationTestInline]
  945. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  946. def test_proxy_model_parent(self):
  947. class Parent(Model):
  948. pass
  949. class ProxyChild(Parent):
  950. class Meta:
  951. proxy = True
  952. class ProxyProxyChild(ProxyChild):
  953. class Meta:
  954. proxy = True
  955. class Related(Model):
  956. proxy_child = ForeignKey(ProxyChild, on_delete=CASCADE)
  957. class InlineFkName(admin.TabularInline):
  958. model = Related
  959. fk_name = "proxy_child"
  960. class InlineNoFkName(admin.TabularInline):
  961. model = Related
  962. class ProxyProxyChildAdminFkName(admin.ModelAdmin):
  963. inlines = [InlineFkName, InlineNoFkName]
  964. self.assertIsValid(ProxyProxyChildAdminFkName, ProxyProxyChild)
  965. class ExtraCheckTests(CheckTestCase):
  966. def test_not_integer(self):
  967. class ValidationTestInline(TabularInline):
  968. model = ValidationTestInlineModel
  969. extra = "hello"
  970. class TestModelAdmin(ModelAdmin):
  971. inlines = [ValidationTestInline]
  972. self.assertIsInvalid(
  973. TestModelAdmin,
  974. ValidationTestModel,
  975. "The value of 'extra' must be an integer.",
  976. "admin.E203",
  977. invalid_obj=ValidationTestInline,
  978. )
  979. def test_valid_case(self):
  980. class ValidationTestInline(TabularInline):
  981. model = ValidationTestInlineModel
  982. extra = 2
  983. class TestModelAdmin(ModelAdmin):
  984. inlines = [ValidationTestInline]
  985. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  986. class MaxNumCheckTests(CheckTestCase):
  987. def test_not_integer(self):
  988. class ValidationTestInline(TabularInline):
  989. model = ValidationTestInlineModel
  990. max_num = "hello"
  991. class TestModelAdmin(ModelAdmin):
  992. inlines = [ValidationTestInline]
  993. self.assertIsInvalid(
  994. TestModelAdmin,
  995. ValidationTestModel,
  996. "The value of 'max_num' must be an integer.",
  997. "admin.E204",
  998. invalid_obj=ValidationTestInline,
  999. )
  1000. def test_valid_case(self):
  1001. class ValidationTestInline(TabularInline):
  1002. model = ValidationTestInlineModel
  1003. max_num = 2
  1004. class TestModelAdmin(ModelAdmin):
  1005. inlines = [ValidationTestInline]
  1006. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  1007. class MinNumCheckTests(CheckTestCase):
  1008. def test_not_integer(self):
  1009. class ValidationTestInline(TabularInline):
  1010. model = ValidationTestInlineModel
  1011. min_num = "hello"
  1012. class TestModelAdmin(ModelAdmin):
  1013. inlines = [ValidationTestInline]
  1014. self.assertIsInvalid(
  1015. TestModelAdmin,
  1016. ValidationTestModel,
  1017. "The value of 'min_num' must be an integer.",
  1018. "admin.E205",
  1019. invalid_obj=ValidationTestInline,
  1020. )
  1021. def test_valid_case(self):
  1022. class ValidationTestInline(TabularInline):
  1023. model = ValidationTestInlineModel
  1024. min_num = 2
  1025. class TestModelAdmin(ModelAdmin):
  1026. inlines = [ValidationTestInline]
  1027. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  1028. class FormsetCheckTests(CheckTestCase):
  1029. def test_invalid_type(self):
  1030. class FakeFormSet:
  1031. pass
  1032. class ValidationTestInline(TabularInline):
  1033. model = ValidationTestInlineModel
  1034. formset = FakeFormSet
  1035. class TestModelAdmin(ModelAdmin):
  1036. inlines = [ValidationTestInline]
  1037. self.assertIsInvalid(
  1038. TestModelAdmin,
  1039. ValidationTestModel,
  1040. "The value of 'formset' must inherit from 'BaseModelFormSet'.",
  1041. "admin.E206",
  1042. invalid_obj=ValidationTestInline,
  1043. )
  1044. def test_inline_without_formset_class(self):
  1045. class ValidationTestInlineWithoutFormsetClass(TabularInline):
  1046. model = ValidationTestInlineModel
  1047. formset = "Not a FormSet Class"
  1048. class TestModelAdminWithoutFormsetClass(ModelAdmin):
  1049. inlines = [ValidationTestInlineWithoutFormsetClass]
  1050. self.assertIsInvalid(
  1051. TestModelAdminWithoutFormsetClass,
  1052. ValidationTestModel,
  1053. "The value of 'formset' must inherit from 'BaseModelFormSet'.",
  1054. "admin.E206",
  1055. invalid_obj=ValidationTestInlineWithoutFormsetClass,
  1056. )
  1057. def test_valid_case(self):
  1058. class RealModelFormSet(BaseModelFormSet):
  1059. pass
  1060. class ValidationTestInline(TabularInline):
  1061. model = ValidationTestInlineModel
  1062. formset = RealModelFormSet
  1063. class TestModelAdmin(ModelAdmin):
  1064. inlines = [ValidationTestInline]
  1065. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  1066. class ListDisplayEditableTests(CheckTestCase):
  1067. def test_list_display_links_is_none(self):
  1068. """
  1069. list_display and list_editable can contain the same values
  1070. when list_display_links is None
  1071. """
  1072. class ProductAdmin(ModelAdmin):
  1073. list_display = ["name", "slug", "pub_date"]
  1074. list_editable = list_display
  1075. list_display_links = None
  1076. self.assertIsValid(ProductAdmin, ValidationTestModel)
  1077. def test_list_display_first_item_same_as_list_editable_first_item(self):
  1078. """
  1079. The first item in list_display can be the same as the first in
  1080. list_editable.
  1081. """
  1082. class ProductAdmin(ModelAdmin):
  1083. list_display = ["name", "slug", "pub_date"]
  1084. list_editable = ["name", "slug"]
  1085. list_display_links = ["pub_date"]
  1086. self.assertIsValid(ProductAdmin, ValidationTestModel)
  1087. def test_list_display_first_item_in_list_editable(self):
  1088. """
  1089. The first item in list_display can be in list_editable as long as
  1090. list_display_links is defined.
  1091. """
  1092. class ProductAdmin(ModelAdmin):
  1093. list_display = ["name", "slug", "pub_date"]
  1094. list_editable = ["slug", "name"]
  1095. list_display_links = ["pub_date"]
  1096. self.assertIsValid(ProductAdmin, ValidationTestModel)
  1097. def test_list_display_first_item_same_as_list_editable_no_list_display_links(self):
  1098. """
  1099. The first item in list_display cannot be the same as the first item
  1100. in list_editable if list_display_links is not defined.
  1101. """
  1102. class ProductAdmin(ModelAdmin):
  1103. list_display = ["name"]
  1104. list_editable = ["name"]
  1105. self.assertIsInvalid(
  1106. ProductAdmin,
  1107. ValidationTestModel,
  1108. "The value of 'list_editable[0]' refers to the first field "
  1109. "in 'list_display' ('name'), which cannot be used unless "
  1110. "'list_display_links' is set.",
  1111. id="admin.E124",
  1112. )
  1113. def test_list_display_first_item_in_list_editable_no_list_display_links(self):
  1114. """
  1115. The first item in list_display cannot be in list_editable if
  1116. list_display_links isn't defined.
  1117. """
  1118. class ProductAdmin(ModelAdmin):
  1119. list_display = ["name", "slug", "pub_date"]
  1120. list_editable = ["slug", "name"]
  1121. self.assertIsInvalid(
  1122. ProductAdmin,
  1123. ValidationTestModel,
  1124. "The value of 'list_editable[1]' refers to the first field "
  1125. "in 'list_display' ('name'), which cannot be used unless "
  1126. "'list_display_links' is set.",
  1127. id="admin.E124",
  1128. )
  1129. def test_both_list_editable_and_list_display_links(self):
  1130. class ProductAdmin(ModelAdmin):
  1131. list_editable = ("name",)
  1132. list_display = ("name",)
  1133. list_display_links = ("name",)
  1134. self.assertIsInvalid(
  1135. ProductAdmin,
  1136. ValidationTestModel,
  1137. "The value of 'name' cannot be in both 'list_editable' and "
  1138. "'list_display_links'.",
  1139. id="admin.E123",
  1140. )
  1141. class AutocompleteFieldsTests(CheckTestCase):
  1142. def test_autocomplete_e036(self):
  1143. class Admin(ModelAdmin):
  1144. autocomplete_fields = "name"
  1145. self.assertIsInvalid(
  1146. Admin,
  1147. Band,
  1148. msg="The value of 'autocomplete_fields' must be a list or tuple.",
  1149. id="admin.E036",
  1150. invalid_obj=Admin,
  1151. )
  1152. def test_autocomplete_e037(self):
  1153. class Admin(ModelAdmin):
  1154. autocomplete_fields = ("nonexistent",)
  1155. self.assertIsInvalid(
  1156. Admin,
  1157. ValidationTestModel,
  1158. msg=(
  1159. "The value of 'autocomplete_fields[0]' refers to 'nonexistent', "
  1160. "which is not a field of 'modeladmin.ValidationTestModel'."
  1161. ),
  1162. id="admin.E037",
  1163. invalid_obj=Admin,
  1164. )
  1165. def test_autocomplete_e38(self):
  1166. class Admin(ModelAdmin):
  1167. autocomplete_fields = ("name",)
  1168. self.assertIsInvalid(
  1169. Admin,
  1170. ValidationTestModel,
  1171. msg=(
  1172. "The value of 'autocomplete_fields[0]' must be a foreign "
  1173. "key or a many-to-many field."
  1174. ),
  1175. id="admin.E038",
  1176. invalid_obj=Admin,
  1177. )
  1178. def test_autocomplete_e039(self):
  1179. class Admin(ModelAdmin):
  1180. autocomplete_fields = ("band",)
  1181. self.assertIsInvalid(
  1182. Admin,
  1183. Song,
  1184. msg=(
  1185. 'An admin for model "Band" has to be registered '
  1186. "to be referenced by Admin.autocomplete_fields."
  1187. ),
  1188. id="admin.E039",
  1189. invalid_obj=Admin,
  1190. )
  1191. def test_autocomplete_e040(self):
  1192. class NoSearchFieldsAdmin(ModelAdmin):
  1193. pass
  1194. class AutocompleteAdmin(ModelAdmin):
  1195. autocomplete_fields = ("featuring",)
  1196. site = AdminSite()
  1197. site.register(Band, NoSearchFieldsAdmin)
  1198. self.assertIsInvalid(
  1199. AutocompleteAdmin,
  1200. Song,
  1201. msg=(
  1202. 'NoSearchFieldsAdmin must define "search_fields", because '
  1203. "it's referenced by AutocompleteAdmin.autocomplete_fields."
  1204. ),
  1205. id="admin.E040",
  1206. invalid_obj=AutocompleteAdmin,
  1207. admin_site=site,
  1208. )
  1209. def test_autocomplete_is_valid(self):
  1210. class SearchFieldsAdmin(ModelAdmin):
  1211. search_fields = "name"
  1212. class AutocompleteAdmin(ModelAdmin):
  1213. autocomplete_fields = ("featuring",)
  1214. site = AdminSite()
  1215. site.register(Band, SearchFieldsAdmin)
  1216. self.assertIsValid(AutocompleteAdmin, Song, admin_site=site)
  1217. def test_autocomplete_is_onetoone(self):
  1218. class UserAdmin(ModelAdmin):
  1219. search_fields = ("name",)
  1220. class Admin(ModelAdmin):
  1221. autocomplete_fields = ("best_friend",)
  1222. site = AdminSite()
  1223. site.register(User, UserAdmin)
  1224. self.assertIsValid(Admin, ValidationTestModel, admin_site=site)
  1225. class ActionsCheckTests(CheckTestCase):
  1226. def test_custom_permissions_require_matching_has_method(self):
  1227. @admin.action(permissions=["custom"])
  1228. def custom_permission_action(modeladmin, request, queryset):
  1229. pass
  1230. class BandAdmin(ModelAdmin):
  1231. actions = (custom_permission_action,)
  1232. self.assertIsInvalid(
  1233. BandAdmin,
  1234. Band,
  1235. "BandAdmin must define a has_custom_permission() method for the "
  1236. "custom_permission_action action.",
  1237. id="admin.E129",
  1238. )
  1239. def test_actions_not_unique(self):
  1240. @admin.action
  1241. def action(modeladmin, request, queryset):
  1242. pass
  1243. class BandAdmin(ModelAdmin):
  1244. actions = (action, action)
  1245. self.assertIsInvalid(
  1246. BandAdmin,
  1247. Band,
  1248. "__name__ attributes of actions defined in BandAdmin must be "
  1249. "unique. Name 'action' is not unique.",
  1250. id="admin.E130",
  1251. )
  1252. def test_actions_unique(self):
  1253. @admin.action
  1254. def action1(modeladmin, request, queryset):
  1255. pass
  1256. @admin.action
  1257. def action2(modeladmin, request, queryset):
  1258. pass
  1259. class BandAdmin(ModelAdmin):
  1260. actions = (action1, action2)
  1261. self.assertIsValid(BandAdmin, Band)