我在Django 1.8工作。我想使用LayerMapping import utility来更新现有模型。
这是我的模特档案:
class PCT(models.Model):
code = models.CharField(max_length=3, primary_key=True,
help_text='Primary care trust code')
ons_code = models.CharField(max_length=9, null=True, blank=True)
name = models.CharField(max_length=200, null=True, blank=True)
boundary = models.GeometryField(null=True, blank=True)
objects = models.GeoManager()
我已经在模型中有一行code: 03V
和name: Corby
,没有边界。
现在我想从KML文件导入此行的某些边界。这是我的导入命令:
class Command(BaseCommand):
args = ''
help = 'Imports boundaries from KML.'
def handle(self, *args, **options):
filename = 'CCC_Feb2013.KML'
ds = DataSource(filename)
layer_mapping = {
'code': 'Name',
'boundary': 'Unknown'
}
lm = LayerMapping(PCT, filename, layer_mapping, transform=False)
lm.save(strict=True, progress=1, verbose=True)
我遇到的问题是,这似乎擦除了现有行,并创建了一个没有name
字段的新行。有没有办法使用LayerMapping更新行,而不是覆盖它?
以下是KML的示例,如果这有助于测试:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Folder>
<description><![CDATA[CCG boundary BSC]]></description>
<Placemark>
<name><![CDATA[03V]]></name>
<description><![CDATA[<br><br><br>
<table border="1" padding="0">
<tr><td>CCGcode</td><td>03V</td></tr>
<tr><td>CCGname</td><td>NHS Corby CCG</td></tr>
]]></description>
<visibility>1</visibility>
<open>0</open>
<Style><LineStyle><color>FF000000</color><width> 1</width></LineStyle>
<PolyStyle><fill>0</fill><outline>1</outline></PolyStyle></Style>
<Polygon>
<extrude>1</extrude>
<altitudeMode>clampToGround</altitudeMode>
<tessellate>1</tessellate>
<outerBoundaryIs><LinearRing>
<coordinates>
-.596387,52.496896,0
-.609296,52.508583,0...
</coordinates>
</LinearRing></outerBoundaryIs>
</Polygon>
</Placemark>
...
</Folder></kml>
如果我不能使用LayerMapping,请解释如何从KML文件导入边界,而不使用LayerMapping?