tests.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from django.contrib import admin
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.test import TestCase
  6. from django.test.utils import str_prefix
  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. SongAdmin.validate(Song)
  33. def test_custom_modelforms_with_fields_fieldsets(self):
  34. """
  35. # Regression test for #8027: custom ModelForms with fields/fieldsets
  36. """
  37. ValidFields.validate(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. ValidFormFieldsets.validate(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. ExcludedFields1.validate,
  54. 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. ExcludedFields2.validate,
  61. 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. ExcludedFieldsAlbumAdmin.validate,
  72. 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. AlbumAdmin.validate,
  87. 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. RawIdNonexistingAdmin.validate,
  97. 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. class MyAdmin(admin.ModelAdmin):
  109. inlines = [TwoAlbumFKAndAnEInline]
  110. MyAdmin.validate(Album)
  111. def test_inline_self_validation(self):
  112. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  113. model = TwoAlbumFKAndAnE
  114. class MyAdmin(admin.ModelAdmin):
  115. inlines = [TwoAlbumFKAndAnEInline]
  116. self.assertRaisesMessage(Exception,
  117. "<class 'admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'admin_validation.models.Album'>",
  118. MyAdmin.validate, Album)
  119. def test_inline_with_specified(self):
  120. class TwoAlbumFKAndAnEInline(admin.TabularInline):
  121. model = TwoAlbumFKAndAnE
  122. fk_name = "album1"
  123. class MyAdmin(admin.ModelAdmin):
  124. inlines = [TwoAlbumFKAndAnEInline]
  125. MyAdmin.validate(Album)
  126. def test_readonly(self):
  127. class SongAdmin(admin.ModelAdmin):
  128. readonly_fields = ("title",)
  129. SongAdmin.validate(Song)
  130. def test_readonly_on_method(self):
  131. def my_function(obj):
  132. pass
  133. class SongAdmin(admin.ModelAdmin):
  134. readonly_fields = (my_function,)
  135. SongAdmin.validate(Song)
  136. def test_readonly_on_modeladmin(self):
  137. class SongAdmin(admin.ModelAdmin):
  138. readonly_fields = ("readonly_method_on_modeladmin",)
  139. def readonly_method_on_modeladmin(self, obj):
  140. pass
  141. SongAdmin.validate(Song)
  142. def test_readonly_method_on_model(self):
  143. class SongAdmin(admin.ModelAdmin):
  144. readonly_fields = ("readonly_method_on_model",)
  145. SongAdmin.validate(Song)
  146. def test_nonexistant_field(self):
  147. class SongAdmin(admin.ModelAdmin):
  148. readonly_fields = ("title", "nonexistant")
  149. self.assertRaisesMessage(ImproperlyConfigured,
  150. str_prefix("SongAdmin.readonly_fields[1], %(_)s'nonexistant' is not a callable "
  151. "or an attribute of 'SongAdmin' or found in the model 'Song'."),
  152. SongAdmin.validate,
  153. Song)
  154. def test_nonexistant_field_on_inline(self):
  155. class CityInline(admin.TabularInline):
  156. model = City
  157. readonly_fields=['i_dont_exist'] # Missing attribute
  158. self.assertRaisesMessage(ImproperlyConfigured,
  159. str_prefix("CityInline.readonly_fields[0], %(_)s'i_dont_exist' is not a callable "
  160. "or an attribute of 'CityInline' or found in the model 'City'."),
  161. CityInline.validate,
  162. City)
  163. def test_extra(self):
  164. class SongAdmin(admin.ModelAdmin):
  165. def awesome_song(self, instance):
  166. if instance.title == "Born to Run":
  167. return "Best Ever!"
  168. return "Status unknown."
  169. SongAdmin.validate(Song)
  170. def test_readonly_lambda(self):
  171. class SongAdmin(admin.ModelAdmin):
  172. readonly_fields = (lambda obj: "test",)
  173. SongAdmin.validate(Song)
  174. def test_graceful_m2m_fail(self):
  175. """
  176. Regression test for #12203/#12237 - Fail more gracefully when a M2M field that
  177. specifies the 'through' option is included in the 'fields' or the 'fieldsets'
  178. ModelAdmin options.
  179. """
  180. class BookAdmin(admin.ModelAdmin):
  181. fields = ['authors']
  182. self.assertRaisesMessage(ImproperlyConfigured,
  183. "'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
  184. BookAdmin.validate,
  185. Book)
  186. def test_cannot_include_through(self):
  187. class FieldsetBookAdmin(admin.ModelAdmin):
  188. fieldsets = (
  189. ('Header 1', {'fields': ('name',)}),
  190. ('Header 2', {'fields': ('authors',)}),
  191. )
  192. self.assertRaisesMessage(ImproperlyConfigured,
  193. "'FieldsetBookAdmin.fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",
  194. FieldsetBookAdmin.validate,
  195. Book)
  196. def test_nested_fields(self):
  197. class NestedFieldsAdmin(admin.ModelAdmin):
  198. fields = ('price', ('name', 'subtitle'))
  199. NestedFieldsAdmin.validate(Book)
  200. def test_nested_fieldsets(self):
  201. class NestedFieldsetAdmin(admin.ModelAdmin):
  202. fieldsets = (
  203. ('Main', {'fields': ('price', ('name', 'subtitle'))}),
  204. )
  205. NestedFieldsetAdmin.validate(Book)
  206. def test_explicit_through_override(self):
  207. """
  208. Regression test for #12209 -- If the explicitly provided through model
  209. is specified as a string, the admin should still be able use
  210. Model.m2m_field.through
  211. """
  212. class AuthorsInline(admin.TabularInline):
  213. model = Book.authors.through
  214. class BookAdmin(admin.ModelAdmin):
  215. inlines = [AuthorsInline]
  216. # If the through model is still a string (and hasn't been resolved to a model)
  217. # the validation will fail.
  218. BookAdmin.validate(Book)
  219. def test_non_model_fields(self):
  220. """
  221. Regression for ensuring ModelAdmin.fields can contain non-model fields
  222. that broke with r11737
  223. """
  224. class SongForm(forms.ModelForm):
  225. extra_data = forms.CharField()
  226. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  227. form = SongForm
  228. fields = ['title', 'extra_data']
  229. FieldsOnFormOnlyAdmin.validate(Song)
  230. def test_non_model_first_field(self):
  231. """
  232. Regression for ensuring ModelAdmin.field can handle first elem being a
  233. non-model field (test fix for UnboundLocalError introduced with r16225).
  234. """
  235. class SongForm(forms.ModelForm):
  236. extra_data = forms.CharField()
  237. class Meta:
  238. model = Song
  239. fields = '__all__'
  240. class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
  241. form = SongForm
  242. fields = ['extra_data', 'title']
  243. FieldsOnFormOnlyAdmin.validate(Song)