Sunday, March 20, 2022

Re: how to split in multiple file class models.py?

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

On Monday, February 1, 2021 at 11:45:25 PM UTC-8 andrea...@hypercode.se wrote:
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


Den tis 2 feb. 2021 kl 07:27 skrev Benny M <chib...@outlook.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.
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.

--
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.

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4d7d9586-5ff6-4012-aa55-96e9f4ab6a5en%40googlegroups.com.

No comments:

Post a Comment