For backwards-compatibility, exclude several types of fields from model validation. See tickets #12507, #12521, #12553.
(self)
| 394 | apply_limit_choices_to_to_formfield(formfield) |
| 395 | |
| 396 | def _get_validation_exclusions(self): |
| 397 | """ |
| 398 | For backwards-compatibility, exclude several types of fields from model |
| 399 | validation. See tickets #12507, #12521, #12553. |
| 400 | """ |
| 401 | exclude = set() |
| 402 | # Build up a list of fields that should be excluded from model field |
| 403 | # validation and unique checks. |
| 404 | for f in self.instance._meta.fields: |
| 405 | field = f.name |
| 406 | # Exclude fields that aren't on the form. The developer may be |
| 407 | # adding these values to the model after form validation. |
| 408 | if field not in self.fields: |
| 409 | exclude.add(f.name) |
| 410 | |
| 411 | # Don't perform model validation on fields that were defined |
| 412 | # manually on the form and excluded via the ModelForm's Meta |
| 413 | # class. See #12901. |
| 414 | elif self._meta.fields and field not in self._meta.fields: |
| 415 | exclude.add(f.name) |
| 416 | elif self._meta.exclude and field in self._meta.exclude: |
| 417 | exclude.add(f.name) |
| 418 | |
| 419 | # Exclude fields that failed form validation. There's no need for |
| 420 | # the model fields to validate them as well. |
| 421 | elif field in self._errors: |
| 422 | exclude.add(f.name) |
| 423 | |
| 424 | # Exclude empty fields that are not required by the form, if the |
| 425 | # underlying model field is required. This keeps the model field |
| 426 | # from raising a required error. Note: don't exclude the field from |
| 427 | # validation if the model field allows blanks. If it does, the |
| 428 | # blank value may be included in a unique check, so cannot be |
| 429 | # excluded from validation. |
| 430 | else: |
| 431 | form_field = self.fields[field] |
| 432 | field_value = self.cleaned_data.get(field) |
| 433 | if ( |
| 434 | not f.blank |
| 435 | and not form_field.required |
| 436 | and field_value in form_field.empty_values |
| 437 | ): |
| 438 | exclude.add(f.name) |
| 439 | return exclude |
| 440 | |
| 441 | def clean(self): |
| 442 | self._validate_unique = True |
no test coverage detected