Django

Code

Changeset 7214

Show
Ignore:
Timestamp:
03/10/08 16:13:52 (4 months ago)
Author:
jbronn
Message:

gis: LayerMapping: OGR string fields may now be coerced into Django IntegerField; added support for more Django field types.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/utils/layermapping.py

    r7028 r7214  
    99 into a GeoDjango model. 
    1010 
    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. 
    1312 
    1413 Requirements:  OGR C Library (from GDAL) required. 
     
    112111from decimal import Decimal 
    113112from django.core.exceptions import ObjectDoesNotExist 
    114 from django.contrib.gis.db.models.fields import GeometryField 
     113from django.contrib.gis.db.models import GeometryField 
    115114from django.contrib.gis.db.backend import SpatialBackend 
    116115from django.contrib.gis.gdal import CoordTransform, DataSource, \ 
     
    141140    FIELD_TYPES = { 
    142141        models.AutoField : OFTInteger, 
    143         models.IntegerField : (OFTInteger, OFTReal), 
     142        models.IntegerField : (OFTInteger, OFTReal, OFTString), 
    144143        models.FloatField : (OFTInteger, OFTReal), 
    145144        models.DateField : OFTDate, 
    146145        models.DateTimeField : OFTDateTime, 
     146        models.EmailField : OFTString, 
    147147        models.TimeField : OFTTime, 
    148148        models.DecimalField : (OFTInteger, OFTReal), 
    149149        models.CharField : OFTString, 
     150        models.SlugField : OFTString, 
    150151        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), 
    153157        } 
    154158 
     
    311315                # Is the OGR field in the Layer? 
    312316                idx = check_ogr_fld(ogr_name) 
    313                  
     317                ogr_field = ogr_field_types[idx] 
     318 
    314319                # 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__]): 
    316321                    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)) 
    318323                fields_val = model_field 
    319324         
     
    398403        otherwise the proper exception is raised. 
    399404        """ 
    400         if isinstance(ogr_field, OFTString): 
     405        if (isinstance(ogr_field, OFTString) and  
     406            isinstance(model_field, (models.CharField, models.TextField))):  
    401407            if self.encoding: 
    402408                # The encoding for OGR data sources may be specified here 
     
    436442                                     (model_field.max_digits, model_field.decimal_places, max_prec)) 
    437443            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. 
    441446            try: 
    442447                val = int(ogr_field.value)