how to split in multiple file class models.py?

183 views
Skip to first unread message

Manuel

unread,
Jan 30, 2021, 6:44:20 PM1/30/21
to Django users
Hello everyone, I have a question about Django and the use of namespaces or packages to split the models.py file.
I would like to adopt the Java philosophy in which one file corresponds one class and one db table.
I tried to follow this guide, but it doesn’t work.
https://docs.djangoproject.com/en/3.1/ref/applications/#namespace-packages-as-apps

when I execute this command: "python manage.py makemigrations myapp"

Return this warning: "No changes detected in app"

Is it possible to do this structure?

/models
    __init__py # if i use package, or without for namespace
         #common import
    car.py # class car
    group.py # class group
    manufactor.py # class manufactor
    /otherPackage
        __init__py
            common import
        otherClass.py

I hope you understand what I’d like to do.
Thank you.

Benny M

unread,
Feb 2, 2021, 1:27:19 AM2/2/21
to django...@googlegroups.com
Just a thought, I haven’t tested this: you might be able to trick access to subdirectories by importing those models in the top-level models/__init__.py - now that I think about it, you might have to do that with all the models... so your __init__ file would say something like:

```
from .car import SomeModel
from .otherPackage.other_model import SomeOtherModel
# and so on ...
```

Again, I could be way off here. I’ve never looked at the magic that parses the models on migration creation.

Best,
Benny

On Jan 30, 2021, at 5:44 PM, Manuel <emanue...@gmail.com> wrote:

Hello everyone, I have a question about Django and the use of namespaces or packages to split the models.py file.
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6cdc67de-94cb-4ad3-8c73-bc91da02b8bfn%40googlegroups.com.

Andréas Kühne

unread,
Feb 2, 2021, 2:45:25 AM2/2/21
to django...@googlegroups.com
So,

Boney M's assumption here is correct.

You just need to import all of your models in the __init__.py file using relative imports, so for example:

from .car import Car

and so on.

This works perfectly and I usually try to split the models in that way as not to get a models.py that is over 10000 lines long.

Regards,

Andréas


Andréas Kühne

unread,
Feb 2, 2021, 2:45:42 AM2/2/21
to django...@googlegroups.com
Sorry, BENNY M. Apologies.

Regards,

Andréas

Benny M

unread,
Feb 2, 2021, 2:54:18 AM2/2/21
to django...@googlegroups.com
Not a problem at all, Andréas. Gave me a chuckle.

Best,
Boney

On Feb 2, 2021, at 1:45 AM, Andréas Kühne <andrea...@hypercode.se> wrote:



richard...@gmail.com

unread,
Mar 20, 2022, 6:44:12 PM3/20/22
to Django users
Sorry for reviving an old topic, but I have a models.py file that is >15,000 lines and would like to chop it up (PyCharm is groaning under the weight!).

How would I handle the imports between the chopped-up files in a models module where the referenced foreign key model is in a different file?
Is there a risk of circular imports?

Thanks,
Richard Brockie
Reply all
Reply to author
Forward
0 new messages