python - makemigrations 没有检测到新模型

标签 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,这个模块是全新的代码,之前没有迁移历史。

知道为什么这些更改没有被采纳吗?

最佳答案

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

The manage.py startapp command creates an application structure that includes a models.py file. If you have many models, organizing them in separate files may be useful.

To do so, create a models package. Remove models.py and create a myapp/models/ directory with an __init__.py file and the files to store your models. You must import the models in the __init__.py file. For example, if you had organic.py and synthetic.py in the models directory:

myapp/models/__init__.py

from .organic import Person
from .synthetic import Robot

关于python - makemigrations 没有检测到新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44117796/

相关文章:

python - 下一页和 scrapy 爬虫不起作用

python - Python 类中的 unicode(self) 和 self.__unicode__() 有什么区别?

python - Tensorflow:尝试迁移学习时出错:无效的 JPEG 数据或裁剪窗口

python - 我们如何通过 rest API 或 http 请求创建 Dataproc 集群?

Django UNIQUE 约束失败 : core_organization. 名称

django - 如何在 Django 中创建组权限

python - 在 django 1.2.1 中,我怎样才能得到类似旧的 .as_sql 的东西?

django - 在 django-crispy-forms 字段中添加或附加图标

python - Django生成 'WHERE ... BETWEEN ...'句?

python - 在 Django 中,如何通过单个表单提交同时创建用户和用户配置文件