Index: django/contrib/gis/utils/layermapping.py
===================================================================
--- django/contrib/gis/utils/layermapping.py	(revision 17150)
+++ django/contrib/gis/utils/layermapping.py	(working copy)
@@ -295,7 +295,8 @@
             elif isinstance(model_field, models.base.ModelBase):
                 # The related _model_, not a field was passed in -- indicating
                 # another mapping for the related Model.
-                val = self.verify_fk(feat, model_field, ogr_name)
+                fk_field = self.model._meta.get_field(field_name)
+                val = self.verify_fk(feat, model_field, fk_field, ogr_name)
             else:
                 # Otherwise, verify OGR Field type.
                 val = self.verify_ogr_field(feat[ogr_name], model_field)
@@ -373,7 +374,7 @@
             val = ogr_field.value
         return val
 
-    def verify_fk(self, feat, rel_model, rel_mapping):
+    def verify_fk(self, feat, rel_model, fk_field, rel_mapping):
         """
         Given an OGR Feature, the related model and its dictionary mapping,
         this routine will retrieve the related model for the ForeignKey
@@ -392,7 +393,13 @@
         try:
             return rel_model.objects.using(self.using).get(**fk_kwargs)
         except ObjectDoesNotExist:
-            raise MissingForeignKey('No ForeignKey %s model found with keyword arguments: %s' % (rel_model.__name__, fk_kwargs))
+            # When the foreign key value did not exist and
+            # the foreign key field's configuration allows null foreign key values,
+            # then set the foreign key value to None
+            if fk_field.null:
+                return None
+            else:
+                raise MissingForeignKey('No ForeignKey %s model found with keyword arguments: %s' % (rel_model.__name__, fk_kwargs))
 
     def verify_geom(self, geom, model_field):
         """
Index: django/contrib/gis/tests/layermap/tests.py
===================================================================
--- django/contrib/gis/tests/layermap/tests.py	(revision 17150)
+++ django/contrib/gis/tests/layermap/tests.py	(working copy)
@@ -11,13 +11,14 @@
 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
 
 from .models import (
-    City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State,
+    City, County, CountyNullState, CountyFeat, Interstate, ICity1, ICity2, Invalid, State,
     city_mapping, co_mapping, cofeat_mapping, inter_mapping)
 
 
 shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, 'data'))
 city_shp = os.path.join(shp_path, 'cities', 'cities.shp')
 co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
+co_shp_null_state = os.path.join(shp_path, 'counties', 'counties_null_state.shp')
 inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp')
 invalid_shp = os.path.join(shp_path, 'invalid', 'emptypoints.shp')
 
@@ -26,6 +27,7 @@
 NUMS   = [1, 2, 1, 19, 1] # Number of polygons for each.
 STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
 
+
 class LayerMapTest(TestCase):
 
     def test01_init(self):
@@ -278,3 +280,28 @@
         lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
                           source_srs=4326)
         lm.save(silent=True)
+
+    def test08_null_foreign_key(self):
+        "Tests LayerMapping for models with nullable foreign keys.  See #17018."
+        # Check that a record is successfully added with a null foreign key value when
+        # the model foreign key field allows a null value and mapping value is null
+        lm = LayerMapping(CountyNullState, co_shp_null_state, co_mapping, transform=False, unique='name')
+        lm.save()
+        self.assertEqual(1, CountyNullState.objects.all().count())
+        county = CountyNullState.objects.all()[0]
+        self.assertEqual('Pueblo', county.name)
+        self.assertEqual(None, county.state)
+        CountyNullState.objects.all().delete()
+
+        # Check that records are successfully added with null foreign key values when
+        # the model foreign key field allows a null value and there is no foreign record
+        lm = LayerMapping(CountyNullState, co_shp, co_mapping, transform=False, unique='name')
+        lm.save()
+        self.assertEqual(len(NAMES), CountyNullState.objects.all().count())
+        # Ensure that all of the saved records have state set to None
+        self.assertEqual(len(NAMES), CountyNullState.objects.filter(state=None).count())
+
+        # Check that MissingForeignKey is raised when
+        # the model foreign key field does not allow a null value and mapping value is null
+        lm = LayerMapping(County, co_shp_null_state, co_mapping, transform=False, unique='name')
+        self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True)
Index: django/contrib/gis/tests/layermap/models.py
===================================================================
--- django/contrib/gis/tests/layermap/models.py	(revision 17150)
+++ django/contrib/gis/tests/layermap/models.py	(working copy)
@@ -10,6 +10,12 @@
     mpoly = models.MultiPolygonField(srid=4269) # Multipolygon in NAD83
     objects = models.GeoManager()
 
+class CountyNullState(models.Model):
+    name = models.CharField(max_length=25)
+    state = models.ForeignKey(State, null=True)
+    mpoly = models.MultiPolygonField(srid=4269) # Multipolygon in NAD83
+    objects = models.GeoManager()
+
 class CountyFeat(models.Model):
     name = models.CharField(max_length=25)
     poly = models.PolygonField(srid=4269)
