In Django 3.0 , instead of putting all your models in a single models.py
file like usual:
my_project/
├── my_project/
└── my_app/
├── models.py
...
You can create a models/
folder and put each model in a separate file and call normally after adding it to an __init__.py
file.
my_project/
├── my_project/
└── my_app/
├── models/
│ ├── __init__.py
│ ├── my_models_file.py
│ └── my_other_models_file.py
...
The models/__init__.py
should look like something like this:
from .my_models_file import *
from .my_other_models_file import *
And my models.MyExampleModel
in my my_models.py
can be called normally in my other files like this:
from .models import MyExampleModel
# My code
This is also possible with the views
.
Here is my question:
Is it possible to proceed the same way with non-Django related files?
I have several functions in my views.py
which are not views . They don't render anything. These are simple python functions used to make some of them reusable.
# My imports
# My view rendering something
def my_function(arg):
# Do something
def my_other_function(arg):
# Do something
I would like to put them in a folder and put each function in a separate file. Having something like this:
my_project/
├── my_project/
└── my_app/
├── my_functions/
│ ├── __init__.py
│ ├── my_functions_file.py
│ └── my_other_functions_file.py
...
I tried to import this with something like that without success.
from my_functions.my_functions_file import my_function
def new_function():
my_function(arg)
Is there a similar way to proceed? The purpose of it is to keep the files as clean and readable as possible. I don't like having a lot of not-view related functions in my views.py
and I would like to put them in their own folder.
Thank you for your help. Ask for more information if I wasn't clear.
use this
from my_app.my_functions.my_functions_file import my_function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.