How to split models.py into several files with Python Django?

Sometimes, we want to split models.py into several files with Python Django.

In this article, we’ll look at how to split models.py into several files with Python Django.

How to split models.py into several files with Python Django?

To split models.py into several files with Python Django, we can put the model files in the models folder in our app folder.

And we add the __init__.py file into the root of the models folder.

For instance, we can have this structure in our app

app1/
    views.py
    __init__.py
    models/
        __init__.py
        model1.py
        model2.py

We add __init__.py into app1/models and we add the model class files inside app1/models.

Conclusion

To split models.py into several files with Python Django, we can put the model files in the models folder in our app folder.

And we add the __init__.py file into the root of the models folder.