skip to Main Content

I have the following structure :

DjangoProjectRoot
    API
      Peak
         peak_model.py
      Waterfall
         waterfall_model.py
    models.py
      ...

As you can sea I organized the code to have separate module for each models, containing the model itself as well as the associated serializer and view.

When I delete my database schema, all the migrations "makemigrations", the others tables are created, except the waterfall one.

  api/migrations/0001_initial.py
    - Create model Book
    - Create model Department
    - Create model Natural
    - Create model Publisher
    - Create model Region
    - Create model Cave
    - Create model Glacier
    - Create model Lake
    - Create model Peak
    - Create model Saddle
    - Create model Shelter
    - Create model Municipality
    - Create model Guide
    - Add field region to department
    - Create model Collection
    - Create model Author

Any idea why ?

I just refactored my code, before everything was in the model field. I’m also a bit surpised by " – Add field region to department ". My database is empty and I cleaned the migrations. Maybe I should add something in the init.py file ? Seems like django needs to detect my new structure ?

waterfall_model.py :

from django.db import models
from api.natural.natural_model import Natural
class Waterfall(Natural):
    height = models.IntegerField()

2

Answers


  1. you should register your apps in settings.py

    INSTALLED_APPS = [
        ...
        'API.Peak',
        'API.Waterfall'
        ...
    ]
    

    also don’t forget to rename peak_model.py and waterfall_model.py into modes.py or organize they in models module.

    Login or Signup to reply.
  2. When making migrations, Django goes through all the apps listed in the INSTALLED_APPS, looks at existing migrations to build the migration state, and then compares that to the current status of the models that are available in apps’ models module.

    In order to have them recognized, you’ll either need to have them all in models.py, or, if you’re going with a package (models/ dir with multiple files), you’ll need to import them all in the models/__init__.py file.

    When there is already a migrations/ package present in app’s dir, makemigrations will just pick the models up and generate the new migrations. However, if you do not have a migrations package, Django will not pick it up automatically, and you will have to explicitly specify the apps’ names.

    e.g. If you have an API.Peak app installed, but you do not have a API/Peak/migrations/ folder present, you will have to explicitly provide it, i.e. python manage.py makemigrations Peak (Note that when apps have dots in them, you only need to specify the last part, so API.Peak and API.Waterfall would be designated just with Peak, or Waterfall)


    tl;dr As Maxim Danilov instructed, you’ll need to add those apps into the INSTALLED_APPS, make sure you have an importable models module that contains those modules, and then run makemigrations command again (explicitly specifying app name might be required).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search