makemigrations没有检测到新模型

时间:2017-05-22 16:24:36

标签: python django django-models

Django 1.11。我最近将以下模块添加到myapp.models:

from django.db import models
from ordered_model.models import OrderedModel

from .songs import Song

class Service(models.Model):
    name = models.CharField(max_len=200, unique=True)
    date = models.DateField(blank=True)
    items = models.ManyToManyField(ServiceItem)

    class Meta:
        ordering = ('-date', 'name')

class ServiceItem(OrderedModel):
    item = models.ForeignKey(Song)
    song_custom_order = models.CharField(max_len=50, blank=True)
    order_with_respect_to = 'service'

    class Meta(OrderedModel.Meta):
        pass

我在另一个模块中有其他模型(在同一目录中)。当我运行./manage.py makemigrations时,脚本会在我的其他模块中找到我的更改,但不会在此模块中找到。 FWIW,此模块是所有新代码,没有以前的迁移历史记录。

为什么这些变化没有得到解决的任何想法?

4 个答案:

答案 0 :(得分:5)

必须将模型导入__init__.py包的models文件中。来自docs

  

manage.py startapp命令创建一个应用程序结构   包含 models.py 文件。如果您有许多模型,请将它们组织起来   单独的文件可能很有用。

     

为此,请创建模型包。删除 models.py 并创建一个   带有 __ init __。py 文件的 myapp / models / 目录以及   存储你的模型。您必须在 __ init __。py 文件中导入模型。   例如,如果模型中有 organic.py synthetic.py   目录:

<强>的myapp /模型/ __初始化__。PY

from .organic import Person
from .synthetic import Robot

答案 1 :(得分:3)

尝试运行指定app_name的makemigrations,

python manage.py makemigrations <your_app_name>

./manage.py makemigrations <your_app_name>

这应该可以解决问题。

另外,请确保your_app_name包含在settings.py文件的INSTALLED_APPS设置中。

答案 2 :(得分:0)

将模型目录创建为一个包( init .py应该存在)并在那里存储模型文件可能会有所帮助。

答案 3 :(得分:0)

在添加新模型作为新应用的一部分时,我遇到了这个问题。

解决方案是确保新的应用名称包含在import re query = '|'.join([re.escape(i) for i in ['(gold)', '(silver)']]) df = df[~df['var'].str.contains(query)]

相关问题