GeoDjango教程错误:无法将关键字“ mpoly”解析为字段

时间:2018-07-18 10:40:34

标签: django geodjango

我正在研究geodjango的官方教程。我正在使用:

  • geos 3.6.2
  • proj.datumgrid 1.5
  • proj 4.9.1
  • gdal 2.3.1
  • django 2.0.7
  • PostGIS 2.4

我要开始使用

>>> from mapmaybe.models import WorldBorder
>>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)

它返回此错误跟踪:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1253, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1277, in _add_q
    split_subq=split_subq,
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1153, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1015, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/home/spencer/PycharmProjects/demo/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1379, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'mpoly' into field. Choices are: area, fips, geom, id, iso2, iso3, lat, lon, name, pop2005, region, subregion, un

有什么想法吗?预先感谢

在下面添加了模型:

enter code here

这是由ogrinspect创建的自动生成的Django模型模块。

从django.contrib.gis.db导入模型

class WorldBorder(models.Model):
    fips = models.CharField(max_length=2)
    iso2 = models.CharField(max_length=2)
    iso3 = models.CharField(max_length=3)
    un = models.IntegerField()
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.BigIntegerField()
    region = models.IntegerField()
    subregion = models.IntegerField()
    lon = models.FloatField()
    lat = models.FloatField()
    geom = models.MultiPolygonField(srid=4326)


# Auto-generated `LayerMapping` dictionary for WorldBorder model
worldborder_mapping = {
    'fips': 'FIPS',
    'iso2': 'ISO2',
    'iso3': 'ISO3',
    'un': 'UN',
    'name': 'NAME',
    'area': 'AREA',
    'pop2005': 'POP2005',
    'region': 'REGION',
    'subregion': 'SUBREGION',
    'lon': 'LON',
    'lat': 'LAT',
    'geom': 'MULTIPOLYGON',
}

1 个答案:

答案 0 :(得分:1)

您的模型没有某些字段(例如mpoly)。在GeoDjango中,教程模型如下https://docs.djangoproject.com/en/2.0/ref/contrib/gis/tutorial/#defining-a-geographic-model

from django.contrib.gis.db import models

class WorldBorder(models.Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.IntegerField('Population 2005')
    fips = models.CharField('FIPS Code', max_length=2)
    iso2 = models.CharField('2 Digit ISO', max_length=2)
    iso3 = models.CharField('3 Digit ISO', max_length=3)
    un = models.IntegerField('United Nations Code')
    region = models.IntegerField('Region Code')
    subregion = models.IntegerField('Sub-Region Code')
    lon = models.FloatField()
    lat = models.FloatField()

    # GeoDjango-specific: a geometry field (MultiPolygonField)
    mpoly = models.MultiPolygonField()

    # Returns the string representation of the model.
    def __str__(self):
        return self.name