inspectdb.py 1.4 KB

123456789101112131415161718192021222324252627282930
  1. from django.core.management.commands.inspectdb import Command as InspectDBCommand
  2. class Command(InspectDBCommand):
  3. db_module = 'django.contrib.gis.db'
  4. gis_tables = {}
  5. def get_field_type(self, connection, table_name, row):
  6. field_type, field_params, field_notes = super(Command, self).get_field_type(connection, table_name, row)
  7. if field_type == 'GeometryField':
  8. geo_col = row[0]
  9. # Getting a more specific field type and any additional parameters
  10. # from the `get_geometry_type` routine for the spatial backend.
  11. field_type, geo_params = connection.introspection.get_geometry_type(table_name, geo_col)
  12. field_params.update(geo_params)
  13. # Adding the table name and column to the `gis_tables` dictionary, this
  14. # allows us to track which tables need a GeoManager.
  15. if table_name in self.gis_tables:
  16. self.gis_tables[table_name].append(geo_col)
  17. else:
  18. self.gis_tables[table_name] = [geo_col]
  19. return field_type, field_params, field_notes
  20. def get_meta(self, table_name):
  21. meta_lines = super(Command, self).get_meta(table_name)
  22. if table_name in self.gis_tables:
  23. # If the table is a geographic one, then we need make
  24. # GeoManager the default manager for the model.
  25. meta_lines.insert(0, ' objects = models.GeoManager()')
  26. return meta_lines