2

Django 1.11. I recently added the following module to 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

I have other models in another module (in the same directory). When I run ./manage.py makemigrations, the script finds my changes in my other module, but not in this one. FWIW, this module is all new code with no previous migration history.

Any ideas why these changes aren't getting picked up?

Scott Severance
  • 943
  • 10
  • 27
  • check if you have init.py file and check the file name is models or not – Exprator May 22 '17 at 16:54
  • 1
    the models must be imported in the `__init__.py file of `models` package. See [this answer](https://stackoverflow.com/a/37758721/1418794) for more info. – doru May 22 '17 at 16:56
  • Thanks, @doru. If you turn this into an answer, I'll accept it. It did the trick. I never would have expected to have to import my models in `__init__.py`. – Scott Severance May 23 '17 at 01:57

4 Answers4

7

The models must be imported in the __init__.py file of the models package. From the 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
doru
  • 9,022
  • 2
  • 33
  • 43
5

Try , running makemigrations specifying the app_name,

python manage.py makemigrations <your_app_name>

or

./manage.py makemigrations <your_app_name>

This should do the trick.

Also, make sure your_app_name is included in the INSTALLED_APPS setting in settings.py file.

zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • Thanks, but this answer solves a different problem, wherein none of the migrations work. My problem was solved in the comments above. – Scott Severance May 23 '17 at 01:59
1

I had this problem while adding new models as part of a new app.

The solution is to make sure that the new app name is included in INSTALLED_APPS.

Susanne Peng
  • 867
  • 1
  • 10
  • 16
0

Creation of models directory as a package (init.py should be there) and storing the model files there might do the trick.

tigrank
  • 31
  • 2