Ticket #17018: django-17018.diff
File django-17018.diff, 5.5 KB (added by , 13 years ago) |
---|
-
django/contrib/gis/utils/layermapping.py
295 295 elif isinstance(model_field, models.base.ModelBase): 296 296 # The related _model_, not a field was passed in -- indicating 297 297 # another mapping for the related Model. 298 val = self.verify_fk(feat, model_field, ogr_name) 298 fk_field = self.model._meta.get_field(field_name) 299 val = self.verify_fk(feat, model_field, fk_field, ogr_name) 299 300 else: 300 301 # Otherwise, verify OGR Field type. 301 302 val = self.verify_ogr_field(feat[ogr_name], model_field) … … 373 374 val = ogr_field.value 374 375 return val 375 376 376 def verify_fk(self, feat, rel_model, rel_mapping):377 def verify_fk(self, feat, rel_model, fk_field, rel_mapping): 377 378 """ 378 379 Given an OGR Feature, the related model and its dictionary mapping, 379 380 this routine will retrieve the related model for the ForeignKey … … 385 386 386 387 # Constructing and verifying the related model keyword arguments. 387 388 fk_kwargs = {} 389 ogr_values_null = True 388 390 for field_name, ogr_name in rel_mapping.items(): 391 ogr_values_null = ogr_values_null and feat[ogr_name].value == '' 389 392 fk_kwargs[field_name] = self.verify_ogr_field(feat[ogr_name], rel_model._meta.get_field(field_name)) 390 393 394 # If the all of the ogr values representing the foreign key are None 395 # and the foreign key field's configuration allows null foreign key values, 396 # then set the foreign key value to None 397 if fk_field.null and ogr_values_null: 398 return None 399 391 400 # Attempting to retrieve and return the related model. 392 401 try: 393 402 return rel_model.objects.using(self.using).get(**fk_kwargs) -
django/contrib/gis/tests/layermap/tests.py
11 11 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey 12 12 13 13 from .models import ( 14 City, County, County Feat, Interstate, ICity1, ICity2, Invalid, State,14 City, County, CountyNullState, CountyFeat, Interstate, ICity1, ICity2, Invalid, State, 15 15 city_mapping, co_mapping, cofeat_mapping, inter_mapping) 16 16 17 17 18 18 shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, 'data')) 19 19 city_shp = os.path.join(shp_path, 'cities', 'cities.shp') 20 20 co_shp = os.path.join(shp_path, 'counties', 'counties.shp') 21 co_shp_null_state = os.path.join(shp_path, 'counties', 'counties_null_state.shp') 21 22 inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp') 22 23 invalid_shp = os.path.join(shp_path, 'invalid', 'emptypoints.shp') 23 24 … … 26 27 NUMS = [1, 2, 1, 19, 1] # Number of polygons for each. 27 28 STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado'] 28 29 30 29 31 class LayerMapTest(TestCase): 30 32 31 33 def test01_init(self): … … 278 280 lm = LayerMapping(Invalid, invalid_shp, invalid_mapping, 279 281 source_srs=4326) 280 282 lm.save(silent=True) 283 284 def test08_null_foreign_key(self): 285 "Tests LayerMapping for models with nullable foreign keys. See #17018." 286 # Check that a record is successfully added with a null foreign key value when 287 # the model foreign key field allows a null value and mapping value is null 288 lm = LayerMapping(CountyNullState, co_shp_null_state, co_mapping, transform=False, unique='name') 289 lm.save() 290 self.assertEqual(1, CountyNullState.objects.all().count()) 291 county = CountyNullState.objects.all()[0] 292 self.assertEqual('Pueblo', county.name) 293 self.assertEqual(None, county.state) 294 CountyNullState.objects.all().delete() 295 296 # Check that MissingForeignKey is raised when 297 # the model foreign key field does not allow a null value and mapping value is null 298 lm = LayerMapping(County, co_shp_null_state, co_mapping, transform=False, unique='name') 299 self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True) 300 301 # Check that MissingForeignKey is raised when 302 # the model foreign key field allows a null value and there is no foreign record 303 # There are no states populated in this case 304 lm = LayerMapping(CountyNullState, co_shp, co_mapping, transform=False, unique='name') 305 self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True) -
django/contrib/gis/tests/layermap/models.py
10 10 mpoly = models.MultiPolygonField(srid=4269) # Multipolygon in NAD83 11 11 objects = models.GeoManager() 12 12 13 class CountyNullState(models.Model): 14 name = models.CharField(max_length=25) 15 state = models.ForeignKey(State, null=True) 16 mpoly = models.MultiPolygonField(srid=4269) # Multipolygon in NAD83 17 objects = models.GeoManager() 18 13 19 class CountyFeat(models.Model): 14 20 name = models.CharField(max_length=25) 15 21 poly = models.PolygonField(srid=4269)