Python django抽象模型继承导入
我想知道如何将抽象模型导入到另一个应用程序中 世界要素组织认为:Python django抽象模型继承导入,python,django,inheritance,model,abstract,Python,Django,Inheritance,Model,Abstract,我想知道如何将抽象模型导入到另一个应用程序中 世界要素组织认为: class Location(models.Model): """ Holds x,y coordinates of a virtual 2d map. """ x = models.IntegerField() y = models.IntegerField() class Meta: abstract = True def __unicode__(
class Location(models.Model):
"""
Holds x,y coordinates of a virtual 2d map.
"""
x = models.IntegerField()
y = models.IntegerField()
class Meta:
abstract = True
def __unicode__(self):
return "%s, %s" % (self.x, self.y)
现在在另一个应用程序中,我尝试:
from world_elements.models import Location
class NpcTown(Location):
"""
A town with their coordinates trianinggrounds quest office and all other relevant attributes
"""
# general town information
name = models.CharField(max_length = 63)
flavor = models.TextField(max_length = 511)
guild = models.ForeignKey(NpcGuild)
# locations
trainingground = models.ForeignKey(TrainingGround, null=True)
def __unicode__(self):
return self.name
但现在我得到importorror:无法导入名称位置
如何导入抽象模型?试试看
from world_elements import Location
将类的名称简化一点,以下内容对我很有用 在Django 1.7中,这是本次发布时最新的稳定版本 写作 目录布局
project
\_ apps
\_ __init__.py
\_ A
\_ B
\_ config
\_ __init__.py
\_ settings.py
\_ urls.py
\_ wsgi.py
\_ data
\_ makefile
\_ manage.py
\_ README.md
在上面,appA
包含抽象模型<代码>B使用它,如下所示
如下:
抽象类
然后
混凝土等级
请注意,应用程序、A和B都必须包含\uuuu init\uuuuu.py
文件
不要害怕打破Django目录布局惯例
由manage.py start{app,project}
施加。这样做可以解放你的思想
你会喜欢把事情安排得井井有条
另一件有助于调试模块导入的事情是简单地print
ing
模块已导入。然后,您可以知道实际正在解决的问题。
例如:
from apps.A.models import AModel
print AModel # <class 'apps.A.models.AModel'>
从apps.A.models导入AModel
打印AModel#
以及:
导入应用程序
打印应用程序#
在这样的正常结构中:
my_project
- /my_project
- /settings.py
- /app1
- /models.py
* class Model1...
- /app2
- /models.py
* class Model2...
在app1/models.py中,这对我很有用:
from django.db import models
from my_project.app1.models import Model1
class Model2(Model1):
...
使用Django 11.1为什么要从
世界元素导入。模型
如果模型在世界元素中
?请发布您的项目文件结构抱歉,如果不清楚,我的另一个模型在characters应用程序中,或者在wons应用程序中,那么我会遇到下一个错误:ImportError:无法导入名称位置Dude,你让我开心了一天。我与这个进口问题斗争了相当长的一段时间。是的,我应该重新设计我的文件夹结构。这个问题很老了,我实际上已经忘了。。到目前为止,我找到了一个解决这个问题的方法(我讨厌)。哥们,你可以帮我看看曼斯莫尔的最新消息。还将init.py文件添加到设置映射没有问题:)是的,我将\uuu init\uuuu.py
添加到了设置目录-它在我的设置中,我只是忘记了包含它。刚刚遇到另一个导入错误:(来自apps.heros.models导入Hero。现在我确定该类存在,但当我尝试同步数据库时,我得到了该文件“/home/ikke/panda/Buah/apps/heromes/models.py”,第4行,从apps.towns.models导入城镇文件“/home/ikke/panda/Buah/apps/towns/models.py”(一个模型从英雄导入城镇,另一个英雄从城镇导入…)“,第4行,在from apps.heros.models import Hero中,我是否再次循环导入?现在我想我可以通过使用import apps.heros.models作为app_Hero,然后使用app_heros.Hero来解决这个问题,但它找不到我的模型文件(我在apps map和de app maps中都有init)
import apps
print apps # <module 'apps' from '/home/g33k/gits/checkouts/my/project/apps/__init__.pyc'>
my_project
- /my_project
- /settings.py
- /app1
- /models.py
* class Model1...
- /app2
- /models.py
* class Model2...
from django.db import models
from my_project.app1.models import Model1
class Model2(Model1):
...