introspection.py 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. from MySQLdb.constants import FIELD_TYPE
  2. from django.contrib.gis.gdal import OGRGeomType
  3. from django.db.backends.mysql.introspection import DatabaseIntrospection
  4. class MySQLIntrospection(DatabaseIntrospection):
  5. # Updating the data_types_reverse dictionary with the appropriate
  6. # type for Geometry fields.
  7. data_types_reverse = DatabaseIntrospection.data_types_reverse.copy()
  8. data_types_reverse[FIELD_TYPE.GEOMETRY] = 'GeometryField'
  9. def get_geometry_type(self, table_name, geo_col):
  10. cursor = self.connection.cursor()
  11. try:
  12. # In order to get the specific geometry type of the field,
  13. # we introspect on the table definition using `DESCRIBE`.
  14. cursor.execute('DESCRIBE %s' %
  15. self.connection.ops.quote_name(table_name))
  16. # Increment over description info until we get to the geometry
  17. # column.
  18. for column, typ, null, key, default, extra in cursor.fetchall():
  19. if column == geo_col:
  20. # Using OGRGeomType to convert from OGC name to Django field.
  21. # MySQL does not support 3D or SRIDs, so the field params
  22. # are empty.
  23. field_type = OGRGeomType(typ).django
  24. field_params = {}
  25. break
  26. finally:
  27. cursor.close()
  28. return field_type, field_params