Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *