validation.py 1.1 KB

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