validation.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. class BaseDatabaseValidation:
  2. """Encapsulate backend-specific validation."""
  3. def __init__(self, connection):
  4. self.connection = connection
  5. def check(self, **kwargs):
  6. return []
  7. def check_field(self, field, **kwargs):
  8. errors = []
  9. # Backends may implement a check_field_type() method.
  10. if (
  11. hasattr(self, "check_field_type")
  12. and
  13. # Ignore any related fields.
  14. not getattr(field, "remote_field", None)
  15. ):
  16. # Ignore fields with unsupported features.
  17. db_supports_all_required_features = all(
  18. getattr(self.connection.features, feature, False)
  19. for feature in field.model._meta.required_db_features
  20. )
  21. if db_supports_all_required_features:
  22. field_type = field.db_type(self.connection)
  23. # Ignore non-concrete fields.
  24. if field_type is not None:
  25. errors.extend(self.check_field_type(field, field_type))
  26. return errors