【问题标题】:from models import WorldBorder ImportError: No module named 'models'从模型导入 WorldBorder ImportError:没有名为“模型”的模块
【发布时间】:2016-01-19 17:07:11
【问题描述】:

为什么它显示我在设置 GeoDjango 教程时所遵循的错误,一切都是正确的,为什么数据不会加载

模型类:

from django.contrib.gis.db import models

class WorldBorders(models.Model):
    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), and
    # overriding the default manager with a GeoManager instance.
    mpoly = models.MultiPolygonField()
    objects = models.GeoManager()

    class Meta:
        verbose_name_plural = "World Borders"

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

加载.py

import os
from django.contrib.gis.utils import LayerMapping
from models import WorldBorder 

world_mapping = {
    'fips' : 'FIPS',
    'iso2' : 'ISO2',
    'iso3' : 'ISO3',
    'un' : 'UN',
    'name' : 'NAME',
    'area' : 'AREA',
    'pop2005' : 'POP2005',
    'region' : 'REGION',
    'subregion' : 'SUBREGION',
    'lon' : 'LON',
    'lat' : 'LAT',
    'mpoly' : 'MULTIPOLYGON',
}
world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'TM_WORLD_BORDERS-0.3.shp'))

def run(verbose=True):
    lm = LayerMapping(Pakistan, world_shp, world_mapping,transform=False, encoding='utf8')

    lm.save(strict=True, verbose=verbose)

加载数据时出错

from world import load
Traceback (most recent call last):
  File "/Users/Ismail/mainFolder/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 69, in handle
    self.run_shell(shell=options['interface'])
  File "/Users/Ismail/mainFolder/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 61, in run_shell
    raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Ismail/mainFolder/geodjango/world/load.py", line 3, in <module>
    from models import WorldBorder
ImportError: No module named 'models'
>>> 

Django网站的教程有什么问题

【问题讨论】:

    标签: python django postgresql postgis geodjango


    【解决方案1】:

    在load.py中,改变这一行:

    from models import WorldBorder # shoudn't it be WorldBorders with an s?
    

    到:

    # Assuming your appname is called 'world' and your 
    # WorldBorders class is in world/models.py
    from world.models import WorldBorders 
    

    上面的行可能会根据您的应用程序名称而改变。如果您的应用程序名称是“world”,那么它应该可以工作。如果您的应用程序名称是“ApplicationName”,请执行以下操作:

    from ApplicationName.models import WorldBorders
    

    编辑:根据您的文件结构,我认为您的应用程序可能被称为“geodjango”,所以不妨试试:

    from geodjango.models import WorldBorders
    

    【讨论】:

    • 是的,WorldBorder 类在 world/models.py 和 load.py 也在同一个目录中,我已经改变了但它不能工作同样的错误。从 world.models 导入 WorldBorder
    • @JonasDavid 您应该导入 WorldBorders(带有 s),因为模型有一个 s。当您尝试使用 s(即从 world.models 导入 WorldBorders)时,它是否有效?
    • @JonasDavid,您的应用程序名称是什么? (进入 settings.py 并在那里查看已安装的应用程序)。您是否只有一个应用程序,该应用程序是否称为 world?
    猜你喜欢
    • 2021-08-11
    • 2018-07-19
    • 2018-02-08
    • 2019-02-06
    • 2017-07-11
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多