0

My old structure used to be like this:

models.py
    class Model1
    class Model2
    ...

I have since moved this to

modelsdir
   __init__.py
   model1.py
      class Model1
   model2.py
      class Model2
models.py
   from modelsdir.model1 import Model1
   from modelsdir.model2 import Model2

After this change, makemigraitons and makemigrations myapp no longer detect any changes done on the models. Any idea how to fix this?

EDIT:

I have since moved to this: removed models.py and renamed modelsdir to models

now it looks like so:

models
    __init__.py
        from .model1 import Model1
        from .model2 import Model2
    model1.py
    model2.py

Unfortunately this still doesn't detect any changes.

Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • Add `modelsdir` to `installed apps` in `settings.py`. Rename those `modelsx.py` file back to models.py. If you don't want to have more than one Model class in a file just create another app for it. Make your life easy. – tushortz May 05 '17 at 20:24
  • @Tushortz would having one model class per app even be feasible? That sounds pretty bad considering one app can have well over a dozen models. This approach would mean creating a dozen applications. – Stupid.Fat.Cat May 05 '17 at 20:26
  • Why do you want to store each model in a separate file? Why not stay with `models.py`? – a_guest May 05 '17 at 20:30
  • @Stupid.Fat.Cat That's why its better to just have related models in a single model file. – tushortz May 05 '17 at 20:32
  • @a_guest the file was getting pretty large, there were suggestions to split models.py into multiple files but seems like this approach's bringing more problems than it solves. – Stupid.Fat.Cat May 05 '17 at 20:58
  • With a proper IDE you shouldn't be at trouble with large files. But if you wish to stick with multiple modules what about _defining_ your models in a separate module each and then _importing_ those models in `models.py`? `models.py` would just contain an `import` statement for each model then. Or is this what you did already? Cause that should actually work. – a_guest May 05 '17 at 21:03
  • @a_guest this is already what I have. The problem is that makemigrations isn't detecting any of my changes. – Stupid.Fat.Cat May 05 '17 at 21:15
  • you should reset the migrations try this http://stackoverflow.com/questions/29253399/how-to-reset-migrations-in-django-1-7?answertab=active#tab-top – Szakaria May 05 '17 at 21:16
  • @Stupid.Fat.Cat What version of Django and Python do you use? I just tried moving the models to a separate module within a sample project and it worked. I used Django 1.11 and Python 2.7. Maybe [setting an app label](https://docs.djangoproject.com/en/dev/ref/models/options/#app-label) helps in your case. By the way how does your `INSTALLED_APPS` look like? – a_guest May 05 '17 at 21:26
  • @a_guest I'm using 1.9 on 2.7 because of some celery integrations. Hmm maybe something's wrong with my migrations – Stupid.Fat.Cat May 05 '17 at 21:30
  • @a_guest INSTALLED_APPS does have my app listed there, if that's what you're looking for: – Stupid.Fat.Cat May 05 '17 at 21:36
  • @Stupid.Fat.Cat Are you sure you're editing / importing the correct files? And that you don't override the model's names later on in `models.py`? This shouldn't be due to preceding migrations, those modules are still present in `models.py` as before, the only thing that changed is their `__module__` attribute but AFAIK this is not relevant to django; it goes by the `__name__`. Have you manually changed some parts of the existing migrations? – a_guest May 05 '17 at 21:51
  • @Stupid.Fat.Cat are you still having this issue? – Nathan Jones May 09 '17 at 02:01

1 Answers1

1

I have a django project that is structured this way, and I had to add this in my modelsdir/__init__.py file:

from .model1 import *
from .model2 import *

I also didn't keep the original models.py file in my top-level app folder.

This is on Django 1.10/1.11 and Python 3.

Nathan Jones
  • 4,904
  • 9
  • 44
  • 70