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