PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/management/commands/inspectdb.py

https://code.google.com/p/mango-py/
Python | 32 lines | 22 code | 4 blank | 6 comment | 4 complexity | 87d519c48b3f071d11407cf04d70bc0b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from optparse import make_option
  2. from django.core.management.base import CommandError
  3. from django.core.management.commands.inspectdb import Command as InspectDBCommand
  4. class Command(InspectDBCommand):
  5. db_module = 'django.contrib.gis.db'
  6. gis_tables = {}
  7. def get_field_type(self, connection, table_name, row):
  8. field_type, field_params, field_notes = super(Command, self).get_field_type(connection, table_name, row)
  9. if field_type == 'GeometryField':
  10. geo_col = row[0]
  11. # Getting a more specific field type and any additional parameters
  12. # from the `get_geometry_type` routine for the spatial backend.
  13. field_type, geo_params = connection.introspection.get_geometry_type(table_name, geo_col)
  14. field_params.update(geo_params)
  15. # Adding the table name and column to the `gis_tables` dictionary, this
  16. # allows us to track which tables need a GeoManager.
  17. if table_name in self.gis_tables:
  18. self.gis_tables[table_name].append(geo_col)
  19. else:
  20. self.gis_tables[table_name] = [geo_col]
  21. return field_type, field_params, field_notes
  22. def get_meta(self, table_name):
  23. meta_lines = super(Command, self).get_meta(table_name)
  24. if table_name in self.gis_tables:
  25. # If the table is a geographic one, then we need make
  26. # GeoManager the default manager for the model.
  27. meta_lines.insert(0, ' objects = models.GeoManager()')
  28. return meta_lines