2017-05-22 41 views
0

Django 1.11。我最近增加了以下模塊myapp.models:makemigrations沒有檢測到新的模型

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,這個模塊是所有新的代碼,沒有以前的遷移歷史。

任何想法,爲什麼這些變化沒有得到拾起?

+0

檢查,如果你有init.py文件並檢查文件名是否爲模型 – Exprator

+1

模型必須導入'models'包的'__init__.py文件中。請參閱[此答案](https://stackoverflow.com/a/37758721/1418794)瞭解更多信息。 – doru

+0

謝謝,@doru。如果你把它變成答案,我會接受它。它做了詭計。我從來不希望必須在'__init __。py'中導入我的模型。 –

回答

2

該模型必須導入models包的__init__.py文件中。從docs

manage.py的startApp命令創建一個應用程序結構 包括models.py文件。如果您有多個型號,將它們組織在 單獨的文件中可能會有用。

爲此,請創建一個模型包。刪除models.py,並創建一個 的myapp /模型/目錄與__init__.py文件和文件 儲存您的模型。您必須導入__init__.py文件中的模型。 例如,如果你有organic.pysynthetic.py模型 目錄:

MYAPP /模型/ __ init__.py

from .organic import Person 
from .synthetic import Robot 
1

試,運行makemigrations指定APP_NAME,

python manage.py makemigrations <your_app_name> 

./manage.py makemigrations <your_app_name> 

這應該做的伎倆。

此外,請確保your_app_name包含在settings.py文件的INSTALLED_APPS設置中。

+0

謝謝,但這個答案解決了一個不同的問題,其中沒有任何遷移工作。我的問題在上面的評論中得到了解決。 –

0

將模型目錄創建爲一個包(init .py應該存在)並在其中存儲模型文件可能會有所訣竅。