Django的:如何在另一个模块中创建自定义的模型
问题描述:
可以说我有这样的Django设置:Django的:如何在另一个模块中创建自定义的模型
appA - models.py
- views.py
- etc...
global - models.py
我要的是从global.models
在appA.models
导入模型,使他们作为一个正常的处理由syncdb和南部的appA
模型。
global.models:
from django.db import models
class Foo(models.Model):
#django model stuff
appA.models: 尝试1:
from global.models import *
>>manage.py schemamigration appA --auto
>>does not see Foo
尝试2:
from global.models import Foo
class Foo(Foo):
pass
>>manage.py schemamigration appA --auto
>>Error: One or more models did not validate:
>>appA.Foo: 'foo_ptr' has a relation with model <class 'global.models.Foo'>, which has either not been installed or is abstract.
什么是实现这一目标的正确方法是什么?
答
from .models import Foo
class Foo(models.Model):
#stuff
class Meta:
abstract = True
>> manage.py schemamigration appA --auto- >>已删除模型appA.Foo >>已创建0005_auto__del_foo.py。您现在可以将此迁移应用于:./manage.py migrate runengine – RickyA 2013-03-18 12:48:28
我已经拥有此模型。分解重复使用。 – RickyA 2013-03-18 12:49:03
为什么不把你的Foo作为一个抽象? – catherine 2013-03-18 12:50:33