how to split in multiple file class models.py?

Visto 185 veces
Saltar al primer mensaje no leído

Manuel

no leída,
30 ene 2021, 18:44:2030/1/21
a 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

no leída,
2 feb 2021, 1:27:192/2/21
a 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

no leída,
2 feb 2021, 2:45:252/2/21
a 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

no leída,
2 feb 2021, 2:45:422/2/21
a django...@googlegroups.com
Sorry, BENNY M. Apologies.

Regards,

Andréas

Benny M

no leída,
2 feb 2021, 2:54:182/2/21
a 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

no leída,
20 mar 2022, 18:44:1220/3/22
a 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
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos