diff options
| author | Justin Bronn <jbronn@gmail.com> | 2008-01-18 15:52:37 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2008-01-18 15:52:37 +0000 |
| commit | 3dd6b422ca658eaeddbf499513fb8b0a52b7f41d (patch) | |
| tree | 6e00ec572320b81e31a8206c1fbaa3abf999cc9a | |
| parent | 730b1f6d7a51c6605961de708c0674687424ad77 (diff) | |
gis: `LayerMapping`: Fixed bug that would occur when mapping `OFTReal` fields with precision > 0 to an `IntegerField`.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7024 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/contrib/gis/utils/layermapping.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py index c8aae59102..5475dd6c6e 100644 --- a/django/contrib/gis/utils/layermapping.py +++ b/django/contrib/gis/utils/layermapping.py @@ -124,6 +124,7 @@ from django.db import models, transaction class LayerMapError(Exception): pass class InvalidString(LayerMapError): pass class InvalidDecimal(LayerMapError): pass +class InvalidInteger(LayerMapError): pass class MissingForeignKey(LayerMapError): pass class LayerMapping(object): @@ -406,12 +407,12 @@ class LayerMapping(object): if len(val) > model_field.max_length: raise InvalidString('%s model field maximum string length is %s, given %s characters.' % (model_field.name, model_field.max_length, len(val))) - elif isinstance(ogr_field, OFTReal): + elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.DecimalField): try: # Creating an instance of the Decimal value to use. d = Decimal(str(ogr_field.value)) except: - raise InvalidDecimal('Could not construct decimal from: %s' % ogr_field) + raise InvalidDecimal('Could not construct decimal from: %s' % ogr_field.value) # Getting the decimal value as a tuple. dtup = d.as_tuple() @@ -434,6 +435,13 @@ class LayerMapping(object): raise InvalidDecimal('A DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.' % (model_field.max_digits, model_field.decimal_places, max_prec)) val = d + elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.IntegerField): + # If there's an OFTReal field with precision greater than 0 is mapped to + # an IntegerField, the decimal places will be truncated. + try: + val = int(ogr_field.value) + except: + raise InvalidInteger('Could not construct integer from: %s' % ogr_field.value) else: val = ogr_field.value return val |
