models.py => Mandatory to create tables?

36 views
Skip to first unread message

Benjamin SOULAS

unread,
Sep 7, 2018, 2:06:33 PM9/7/18
to Django users
Hi everyone,

Quick question I do not find the answer: does models have to located into models.py script? It believe not, If I got a project composed of 100 tables, it's weird to me that all models should be located here ...

I tried, in this file, to import a model located somewhere else, but when i run python manage.py makemigrations, nothing happens, any idea?

Kind regards

Benjamin

Gerald Brown

unread,
Sep 7, 2018, 2:18:47 PM9/7/18
to django...@googlegroups.com

There should be a models.py file under each of your applications.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4da5a12-9c9b-42b6-b295-5492658cb046%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Benjamin SOULAS

unread,
Sep 7, 2018, 2:21:53 PM9/7/18
to Django users
Ok, but importing external scripts (but still in the project) should work?

Even adding my model code inside models.py, no migration happens.

Mike Dewhirst

unread,
Sep 7, 2018, 2:25:21 PM9/7/18
to django...@googlegroups.com
Easy

Create a models directory. In there make __init__.py file and in there ...

from .whatever import Whatever
... 100 classes

Then in ./models create 100 files with one model class in each file

Anywhere in the project you can say ...

from app.models import Whatever



Connected by Motorola

Benjamin SOULAS

unread,
Sep 7, 2018, 2:27:07 PM9/7/18
to Django users
Hi Mike (again !),

Okay, but it seems I have a much deeper problem, nothing happens during make migration command, I am thinking on deleting my db and restart from scratch ...

Thanks ! 

Andréas Kühne

unread,
Sep 7, 2018, 2:31:01 PM9/7/18
to django...@googlegroups.com
If you follow the way Mike suggests - migrations will work (this is the way we do it in our plattform). The important thing is that you MUST have a models.py file OR a models module (a directory with the name models and a __init__.py file with the imports from the individual files).

Regards,

Andréas


Benjamin SOULAS

unread,
Sep 7, 2018, 2:35:20 PM9/7/18
to Django users
Ok, so I think I really have a much deeper problem, __init__.py file are there, nothing still happen 

Andréas Kühne

unread,
Sep 7, 2018, 2:44:19 PM9/7/18
to django...@googlegroups.com
So the models directory should look like this:
__init__.py
model1.py

In the __init__.py file you should have the following:
from .model1 import Model1

Then it should "just work".

Regards

Andréas


Benjamin SOULAS

unread,
Sep 7, 2018, 2:55:07 PM9/7/18
to Django users
I agree it should, but it doesn't, better, it does not find my model:

project_structure.png



issue__init__.png






Andréas Kühne

unread,
Sep 7, 2018, 3:08:14 PM9/7/18
to django...@googlegroups.com
Ok - I think you need first of all to decide if you want to use the models module or a models.py file. You are now using both - which won't work. The import that you have written in the __init__.py file shouldn't work either because you are using a relative import to get an absolute file. 

First try to delete the models module and start without that.
Also looking at your layout of your code - the project seems a bit strange (the amount of subdirectories in the api project). This is a rather "ok" way to do it:
https://www.revsys.com/tidbits/recommended-django-project-layout/

And then add the models module if required.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Benjamin SOULAS

unread,
Sep 7, 2018, 3:09:59 PM9/7/18
to Django users
Ok ... Shame ... i found, One day I created a "models" directory to store my models ... So because of this, it didn't make any migrations ...

Sorry for the inconvenience, but now I know I have to take care about my folder naming convention ...

Kind regards

Benjamin.

Andréas Kühne

unread,
Sep 7, 2018, 4:43:59 PM9/7/18
to django...@googlegroups.com
Yes but it is completely OK to have a directory called models to store your models in. That's the way we do it for our project. However you can't have both a directory AND a models.py file.

If you choose to go with the models module (a directory called models), you need to have the __init__.py file and separate files for each model, which you then import in the __init__.py file - like I said earlier.

Hope everything works for you now!

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Benjamin SOULAS

unread,
Sep 10, 2018, 6:41:32 AM9/10/18
to Django users
Hi Andréas,

Yes you are absolutely right, because I am still coding a POC to test some module features, I think I forgot to delete it. Now the make migrations and migrate works, BUT it didn't create the expected table, so still have an issue on that, hope to find quickly what happened?

Regards,

Benjamin

Benjamin SOULAS

unread,
Sep 10, 2018, 7:26:48 AM9/10/18
to Django users
Finally I recreate a new DB and it works now
Reply all
Reply to author
Forward
0 new messages