tests.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. from __future__ import absolute_import
  2. from django import forms
  3. from django.contrib import admin
  4. from django.contrib.admin.validation import validate, validate_inline
  5. from django.core.exceptions import ImproperlyConfigured
  6. from django.test import TestCase
  7. from .models import Song, Book, Album, TwoAlbumFKAndAnE, State, City
  8. class SongForm(forms.ModelForm):
  9. pass
  10. class ValidFields(admin.ModelAdmin):
  11. form = SongForm
  12. fields = ['title']
  13. class ValidFormFieldsets(admin.ModelAdmin):
  14. def get_form(self, request, obj=None, **kwargs):
  15. class ExtraFieldForm(SongForm):
  16. name = forms.CharField(max_length=50)
  17. return ExtraFieldForm
  18. fieldsets = (
  19. (None, {
  20. 'fields': ('name',),
  21. }),
  22. )
  23. class ValidationTestCase(TestCase):
  24. def test_readonly_and_editable(self):
  25. class SongAdmin(admin.ModelAdmin):
  26. readonly_fields = ["original_release"]
  27. fieldsets = [
  28. (None, {
  29. "fields": ["title", "original_release"],
  30. }),
  31. ]
  32. validate(SongAdmin, Song)
  33. def test_custom_modelforms_with_fields_fieldsets(self):
  34. """
  35. # Regression test for #8027: custom ModelForms with fields/fieldsets
  36. """
  37. validate(ValidFields, Song)
  38. def test_custom_get_form_with_fieldsets(self):
  39. """
  40. Ensure that the fieldsets validation is skipped when the ModelAdmin.get_form() method
  41. is overridden.
  42. Refs #19445.
  43. """
  44. validate(ValidFormFieldsets, Song)
  45. def test_exclude_values(self):
  46. """
  47. Tests for basic validation of 'exclude' option values (#12689)
  48. """
  49. class ExcludedFields1(admin.ModelAdmin):
  50. exclude = ('foo')
  51. self.assertRaisesMessage(ImproperlyConfigured,
  52. "'ExcludedFields1.exclude' must be a list or tuple.",
  53. validate,
  54. ExcludedFields1, Book)
  55. def test_exclude_duplicate_values(self):
  56. class ExcludedFields2(admin.ModelAdmin):
  57. exclude = ('name', 'name')
  58. self.assertRaisesMessage(ImproperlyConfigured,
  59. "There are duplicate field(s) in ExcludedFields2.exclude",
  60. validate,
  61. ExcludedFields2, Book)
  62. def test_exclude_in_inline(self):
  63. class ExcludedFieldsInline(admin.TabularInline):
  64. model = Song
  65. exclude = ('foo')
  66. class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
  67. model = Album
  68. inlines = [ExcludedFieldsInline]
  69. self.assertRaisesMessage(ImproperlyConfigured,
  70. "'ExcludedFieldsInline.exclude' must be a list or tuple.",
  71. validate,
  72. ExcludedFieldsAlbumAdmin, Album)
  73. def test_exclude_inline_model_admin(self):
  74. """
  75. # Regression test for #9932 - exclude in InlineModelAdmin
  76. # should not contain the ForeignKey field used in ModelAdmin.model
  77. """
  78. class SongInline(admin.StackedInline):
  79. model = Song
  80. exclude = ['album']
  81. class AlbumAdmin(admin.ModelAdmin):
  82. model = Album
  83. inlines = [SongInline]
  84. self.assertRaisesMessage(ImproperlyConfigured,
  85. "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model admin_validation.Album.",
  86. validate,
  87. AlbumAdmin, Album)
  88. def test_app_label_in_admin_validation(self):
  89. """
  90. Regression test for #15669 - Include app label in admin validation messages
  91. """
  92. class RawIdNonexistingAdmin(admin.ModelAdmin):
  93. raw_id_fields = ('nonexisting',)
  94. self.assertRaisesMessage(ImproperlyConfigured,
  95. "'RawIdNonexistingAdmin.raw_id_fields' refers to field 'nonexisting' that is missing from model 'admin_validation.Album'.",
  96. validate,
  97. RawIdNonexistingAdmin, Album)
  98. def test_fk_exclusion(self):
  99. """
  100. Regression test for #11709 - when testing for fk excluding (when exclude is
  101. given) make sure fk_name is honored or things blow up when there is more
  102. than one fk to the parent model.
  103. """
  104. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  105. model = TwoAlbumFKAndAnE
  106. exclude = ("e",)
  107. fk_name = "album1"
  108. validate_inline(TwoAlbumFKAndAnEInline, None, Album)
  109. def test_inline_self_validation(self):
  110. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  111. model = TwoAlbumFKAndAnE
  112. self.assertRaisesMessage(Exception,
  113. "<class 'admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'admin_validation.models.Album'>",
  114. validate_inline,
  115. TwoAlbumFKAndAnEInline, None, Album)
  116. def test_inline_with_specified(self):
  117. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  118. model = TwoAlbumFKAndAnE
  119. fk_name = "album1"
  120. validate_inline(TwoAlbumFKAndAnEInline, None, Album)
  121. def test_readonly(self):
  122. class SongAdmin(admin.ModelAdmin):
  123. readonly_fields = ("title",)
  124. validate(SongAdmin, Song)
  125. def test_readonly_on_method(self):
  126. def my_function(obj):
  127. pass
  128. class SongAdmin(admin.ModelAdmin):
  129. readonly_fields = (my_function,)
  130. validate(SongAdmin, Song)
  131. def test_readonly_on_modeladmin(self):
  132. class SongAdmin(admin.ModelAdmin):
  133. readonly_fields = ("readonly_method_on_modeladmin",)
  134. def readonly_method_on_modeladmin(self, obj):
  135. pass
  136. validate(SongAdmin, Song)
  137. def test_readonly_method_on_model(self):
  138. class SongAdmin(admin.ModelAdmin):
  139. readonly_fields = ("readonly_method_on_model",)
  140. validate(SongAdmin, Song)
  141. def test_nonexistant_field(self):
  142. class SongAdmin(admin.ModelAdmin):
  143. readonly_fields = ("title", "nonexistant")
  144. self.assertRaisesMessage(ImproperlyConfigured,
  145. "SongAdmin.readonly_fields[1], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.",
  146. validate,
  147. SongAdmin, Song)
  148. def test_nonexistant_field_on_inline(self):
  149. class CityInline(admin.TabularInline):
  150. model = City
  151. readonly_fields=['i_dont_exist'] # Missing attribute
  152. self.assertRaisesMessage(ImproperlyConfigured,
  153. "CityInline.readonly_fields[0], 'i_dont_exist' is not a callable or an attribute of 'CityInline' or found in the model 'City'.",
  154. validate_inline,
  155. CityInline, None, State)
  156. def test_extra(self):
  157. class SongAdmin(admin.ModelAdmin):
  158. def awesome_song(self, instance):
  159. if instance.title == "Born to Run":
  160. return "Best Ever!"
  161. return "Status unknown."
  162. validate(SongAdmin, Song)
  163. def test_readonly_lambda(self):
  164. class SongAdmin(admin.ModelAdmin):
  165. readonly_fields = (lambda obj: "test",)
  166. validate(SongAdmin, Song)
  167. def test_graceful_m2m_fail(self):
  168. """
  169. Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
  170. specifies the 'through' option is included in the 'fields' or the 'fieldsets'
  171. ModelAdmin options.
  172. """
  173. class BookAdmin(admin.ModelAdmin):
  174. fields = ['authors']
  175. self.assertRaisesMessage(ImproperlyConfigured,
  176. "'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
  177. validate,
  178. BookAdmin, Book)
  179. def test_cannot_include_through(self):
  180. class FieldsetBookAdmin(admin.ModelAdmin):
  181. fieldsets = (
  182. ('Header 1', {'fields': ('name',)}),
  183. ('Header 2', {'fields': ('authors',)}),
  184. )
  185. self.assertRaisesMessage(ImproperlyConfigured,
  186. "'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
  187. validate,
  188. FieldsetBookAdmin, Book)
  189. def test_nested_fields(self):
  190. class NestedFieldsAdmin(admin.ModelAdmin):
  191. fields = ('price', ('name', 'subtitle'))
  192. validate(NestedFieldsAdmin, Book)
  193. def test_nested_fieldsets(self):
  194. class NestedFieldsetAdmin(admin.ModelAdmin):
  195. fieldsets = (
  196. ('Main', {'fields': ('price', ('name', 'subtitle'))}),
  197. )
  198. validate(NestedFieldsetAdmin, Book)
  199. def test_explicit_through_override(self):
  200. """
  201. Regression test for #12209 -- If the explicitly provided through model
  202. is specified as a string, the admin should still be able use
  203. Model.m2m_field.through
  204. """
  205. class AuthorsInline(admin.TabularInline):
  206. model = Book.authors.through
  207. class BookAdmin(admin.ModelAdmin):
  208. inlines = [AuthorsInline]
  209. # If the through model is still a string (and hasn't been resolved to a model)
  210. # the validation will fail.
  211. validate(BookAdmin, Book)
  212. def test_non_model_fields(self):
  213. """
  214. Regression for ensuring ModelAdmin.fields can contain non-model fields
  215. that broke with r11737
  216. """
  217. class SongForm(forms.ModelForm):
  218. extra_data = forms.CharField()
  219. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  220. form = SongForm
  221. fields = ['title', 'extra_data']
  222. validate(FieldsOnFormOnlyAdmin, Song)
  223. def test_non_model_first_field(self):
  224. """
  225. Regression for ensuring ModelAdmin.field can handle first elem being a
  226. non-model field (test fix for UnboundLocalError introduced with r16225).
  227. """
  228. class SongForm(forms.ModelForm):
  229. extra_data = forms.CharField()
  230. class Meta:
  231. model = Song
  232. fields = '__all__'
  233. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  234. form = SongForm
  235. fields = ['extra_data', 'title']
  236. validate(FieldsOnFormOnlyAdmin, Song)