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