【问题标题】:Django: How to create a model from a definition in another moduleDjango:如何从另一个模块中的定义创建模型
【发布时间】:2013-03-06 19:18:31
【问题描述】:

假设我有这个 Django 设置:

appA - models.py
     - views.py
     - etc...

global - models.py

我想要的是从 appA.models 中的 global.models 导入模型,以便它们被 syncdb 和 south 视为普通的 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.

实现此目的的正确方法是什么?

【问题讨论】:

    标签: django django-south django-orm django-syncdb


    【解决方案1】:
    from .models import Foo
    
    class Foo(models.Model):
        #stuff
    
        class Meta:
            abstract = True
    

    【讨论】:

    • >>manage.py schemamigration appA --auto- >>删除模型 appA.Foo >>Created 0005_auto__del_foo.py。您现在可以使用以下命令应用此迁移:./manage.py migrate runengine
    • 为什么不把你的 Foo 作为一个摘要呢?
    【解决方案2】:

    global 是一个 python 语句,你不应该用它来命名你的包之一。换个名字试试,别忘了在目录下放一个__init__.py文件,把它变成python包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多