test_checks.py 53 KB

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