我读了很多关于这个主题的博士,博客和其他问题,但我不能正确地回答。我在我的模型.py以及tms.py公司在一个名为maps的模块中
在模型.py在
from maps.tms import tileMapConfig
class LayerMapOptions(models.Model):
layer = models.ForeignKey(Shapefile)
basqui_map = models.ForeignKey(BasquiMap)
position = models.IntegerField(max_length=100, blank=True, null=True)
visible = models.BooleanField(default=False)
styles = models.ManyToManyField(LayerStyle, null=True, blank=True)
def save(self, *args, **kwargs):
super(LayerMapOptions, self).save(*args, **kwargs)
self.basqui_map.date_updated = datetime.now()
self.basqui_map.save()
tileMapConfig(self.basqui_map.pk)
在tms.py公司公司名称:
^{pr2}$这将导致以下错误:
from maps.models import LayerMapOptions ImportError: cannot import name LayerMapOptions
怎样才能不发生冲突地导入这些子模块?在
只需将导入放入使用它的函数中:
或者
^{pr2}$模块只导入一次,所以每次它重新进入函数时,它不会重新导入整个模块,它只会从已经导入的模块中获取它,因此(几乎)没有性能问题。在
一种方法是使用完整的命名空间。在
另一个问题是导入过程中运行了一些全局代码:
^{pr2}$当导入一个模块时,模块中的代码将被执行,因此如果您可以将这一行移到一个函数或方法中,这将延迟该行的执行,直到两个模块都完全导入之后。在
这里有一个更好的解释:
http://effbot.org/zone/import-confusion.htm
相关问题 更多 >
编程相关推荐