0

I started learning Django some days ago and for practice I decided to make a little project.

After I did my model, I tried to map it to a relational model in sqlite3 using django.db. But after I run python manage.py makemigrations <app_name> I get the following errors:

python manage.py makemigrations main
SystemCheckError: System check identified some issues:

ERRORS:
auth.Group_permissions: (fields.E336) The models is used as an intermediate models by '<django.db.models.fields.related.ManyToManyField: permissions>', but it does not have a foreign key to 'Group' or 'Permission'.
auth.User_groups: (fields.E336) The models is used as an intermediate models by '<django.db.models.fields.related.ManyToManyField: groups>', but it does not have a foreign key to 'User' or 'Group'.
auth.User_user_permissions: (fields.E336) The models is used as an intermediate models by '<django.db.models.fields.related.ManyToManyField: user_permissions>', but it does not have a foreign key to 'User' or 'Permission'.
contenttypes.ContentType: (models.E012) 'unique_together' refers to the nonexistent field 'models'.

I'm really confused about what they mean (at least they seem similar). In my model there are actually no ManyToMany relations, and I've never modified any of the classes listed in the error message (User, Group).

I don't know if my model's code is related to this. But I am suspicious about how I structured my project; I've left the models.py file empty and created a new directory "models" with all my model files inside. So:

<m_app>
├── migrations
│  
├── sumbmodels
│   └── __init__.py  <--- imports modelA, modelB, etc
|   └── modelA.py
|   └── modelB.py
|   └── ...
|
├── static
│   └── <my_app>
|
├── templates
│   └── <my_app>
|
├── tests
│   └── __init__.py
|   └── testA.py
|   └── testB.py
|   └── ...
│  
└── models.py  <--- from .submodels import *
│  
└── ...

I'm sorry if this is not much information but given this error I have no idea where to look at.

CimimUxMaio
  • 151
  • 8

1 Answers1

1

First, I would suggest sticking with Django base design - keep all models in models.py.

When number of models becomes big - you can consider splitting such big app into several smaller apps, each responsible only for its functionality. Your django project may contain multiple apps.

In case you want to have models in multiple files - check [this answer] (Split models.py into several files).

Oleg Russkin
  • 4,234
  • 1
  • 8
  • 20
  • About splitting my model into several apps, I don't feel like this would be the best way of working in my project. For example, all the classes and functions declared at my models directory correspond to the same functionality. Of course that each class would have different responsibilities but they all are meant to the same whole thing. – CimimUxMaio Dec 27 '19 at 13:14
  • About [Split models.py into several files](https://stackoverflow.com/questions/6336664/split-models-py-into-several-files), I've done that but I'm still having the same error message :( – CimimUxMaio Dec 27 '19 at 13:18
  • Well, the proposed in referenced answer solution with the contents of `models.py` like `from .submodels.model1 import *` works. Do you export `__all__` in submodels `__init__.py` if you are importing whole module, not each model individually? Try isolating the issue - does it start without this app? – Oleg Russkin Dec 27 '19 at 14:41
  • Added the \_\_all\_\_ variable in \_\_init\_\_.py. Same error. It's the only app in the project at the moment, it is small but I've divided the model into 4 files of about 50 lines each for convenience. – CimimUxMaio Dec 27 '19 at 14:55
  • Now it is about finding line that causes error. The error line hardly indicates it. Need more code or / and you need to isolate the problem code - start your django project without whole app or some of its parts until it works / fails. Maybe these models are referenced in admin.py / views / etc ... – Oleg Russkin Dec 27 '19 at 20:55
  • SOLVED. It seemed that as I edited my directory structure I might have renamed my folder 'models' and I didn't realize that PyCharm actually refactored every Django file that referenced 'model'. So basically I broke everything haha. Re-installing Django solved the problem. – CimimUxMaio Dec 28 '19 at 03:48