Changeset 7214 for django/branches/gis
- Timestamp:
- 03/10/08 16:13:52 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/utils/layermapping.py
r7028 r7214 9 9 into a GeoDjango model. 10 10 11 This utility is still in early stages of development, so its usage 12 is subject to change -- please report any bugs. 11 Please report any bugs encountered using this utility. 13 12 14 13 Requirements: OGR C Library (from GDAL) required. … … 112 111 from decimal import Decimal 113 112 from django.core.exceptions import ObjectDoesNotExist 114 from django.contrib.gis.db.models .fieldsimport GeometryField113 from django.contrib.gis.db.models import GeometryField 115 114 from django.contrib.gis.db.backend import SpatialBackend 116 115 from django.contrib.gis.gdal import CoordTransform, DataSource, \ … … 141 140 FIELD_TYPES = { 142 141 models.AutoField : OFTInteger, 143 models.IntegerField : (OFTInteger, OFTReal ),142 models.IntegerField : (OFTInteger, OFTReal, OFTString), 144 143 models.FloatField : (OFTInteger, OFTReal), 145 144 models.DateField : OFTDate, 146 145 models.DateTimeField : OFTDateTime, 146 models.EmailField : OFTString, 147 147 models.TimeField : OFTTime, 148 148 models.DecimalField : (OFTInteger, OFTReal), 149 149 models.CharField : OFTString, 150 models.SlugField : OFTString, 150 151 models.TextField : OFTString, 151 models.SmallIntegerField : (OFTInteger, OFTReal), 152 models.PositiveSmallIntegerField : (OFTInteger, OFTReal), 152 models.URLField : OFTString, 153 models.USStateField : OFTString, 154 models.XMLField : OFTString, 155 models.SmallIntegerField : (OFTInteger, OFTReal, OFTString), 156 models.PositiveSmallIntegerField : (OFTInteger, OFTReal, OFTString), 153 157 } 154 158 … … 311 315 # Is the OGR field in the Layer? 312 316 idx = check_ogr_fld(ogr_name) 313 317 ogr_field = ogr_field_types[idx] 318 314 319 # Can the OGR field type be mapped to the Django field type? 315 if not issubclass(ogr_field _types[idx], self.FIELD_TYPES[model_field.__class__]):320 if not issubclass(ogr_field, self.FIELD_TYPES[model_field.__class__]): 316 321 raise LayerMapError('OGR field "%s" (of type %s) cannot be mapped to Django %s.' % 317 (ogr_field, ogr_field _types[idx].__name__, fld_name))322 (ogr_field, ogr_field.__name__, fld_name)) 318 323 fields_val = model_field 319 324 … … 398 403 otherwise the proper exception is raised. 399 404 """ 400 if isinstance(ogr_field, OFTString): 405 if (isinstance(ogr_field, OFTString) and 406 isinstance(model_field, (models.CharField, models.TextField))): 401 407 if self.encoding: 402 408 # The encoding for OGR data sources may be specified here … … 436 442 (model_field.max_digits, model_field.decimal_places, max_prec)) 437 443 val = d 438 elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.IntegerField): 439 # If there's an OFTReal field with precision greater than 0 is mapped to 440 # an IntegerField, the decimal places will be truncated. 444 elif isinstance(ogr_field, (OFTReal, OFTString)) and isinstance(model_field, models.IntegerField): 445 # Attempt to convert any OFTReal and OFTString value to an OFTInteger. 441 446 try: 442 447 val = int(ogr_field.value)
