Django development tends to hit cli, but
I often use task runner functions such as Pipenv
because I can't remember the options and it's troublesome.
I use poetry scripts a lot because I'm a poetry
sect. (Note that poetry does not implement the scripts function in the name of task runner function)
Also, with django_extensions
, you can often already use the features you want.
In a past post, I introduced a script that does runserver. [Poetry] Start Django's runserver with Poetry script-Qiita In addition to this, I will introduce the scripts used in my project this time.
django-extensions
django-extensions is an application for Django development that provides a variety of useful features. Install django-extensions and add it to INSTALLED_APPS.
$ pip install django-extensions
INSTALLED_APPS = [
...
'django_extensions', #add to
]
scripts
I'm using Poetry for package management, so I'm using poetry scripts. The advantage of using poetry scripts is that even if you execute it outside the virtual environment, it will enter the virtual environment once and execute the task inside the virtual environment. If you want to check only the test, list only the URL, or even if you do not understand the virtual environment, you can execute it with a single command.
As in the previous post, we will use the subprocess package to execute commands.
shell_plus
Speaking of django-extensions, there is a function called shell_plus. It's an extension of the Django shell, which is quite useful as it completes and pre-imports.
def shell_plus():
cmd = ["python", "manage.py", "shell_plus"]
subprocess.run(cmd)
django-extensions has the ability to output all the URLs of your Django application.
It is very useful when creating API documents.
The --format
option allows you to change the output format.
def url():
cmd = ["python", "manage.py", "show_urls", "--format", "aligned", "--force-color"]
subprocess.run(cmd)
If you use the notes feature of django-extensions,
It extracts TODO, FIXME, BUG, HACK, WARNING, NOTE
, etc. in the py file and HTML file and displays them in a list.
This is sober and convenient, and it is good to be able to confirm the TODO at the beginning of the day with the CLI.
The Path to the file is also displayed, so if it is VS Code, you can open it as it is with Ctrl + click
.
def todo():
cmd = ["python", "manage.py", "notes"]
subprocess.run(cmd)
--Output
$ python manage.py notes
/home/user/workspace/app/web/views.py:
* [ 18] TODO sort filter
/home/user/workspace/app/web/models.py:
* [ 11]TODO exception handling
/home/user/workspace/app/web/forms.py:
* [ 32]Added TODO validation
Django's testing capabilities include parallel execution.
Of course, the number of CPU cores is related to the number of executions, but the number of cores varies depending on the environment.
Use multiprocessing.cpu_count ()
to dynamically get the number of cores and execute tests in parallel.
-v
is verbose.
import multiprocessing
def test():
core_num = multiprocessing.cpu_count()
# core_If you set it to num, the test will not work because the subprocess will be one execution process.
cmd = ["python", "manage.py", "test", "--force-color", "-v", "2", "--parallel", f"{core_num - 1}"]
subprocess.run(cmd)
When resetting the database, all migration files will be deleted. It's easy to create a deletion script because it's done fairly often in the early stages of development.
import os
import glob
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) #Changed by the location of this file
def clean_migration():
migration_files = glob.iglob('**/migrations/[0-9][0-9][0-9][0-9]*.py', recursive=True)
for migration_file in migration_files:
os.remove(os.path.join(BASE_DIR, migration_file))
print(f"Deleted {migration_file}")
Django-extensions provides the ability to delete and recreate a database with a single command.
def reset_db():
cmd = ["python", "manage.py", "reset_db"]
subprocess.run(cmd)
If you use the function of django-extensions, ER diagram will be automatically generated from model definition.
To use this feature, you need Graphviz
and the Python adapter pygraphviz
,
It is great to create the latest ER diagram with one command.
def graph():
cmd = ["python", "manage.py", "graph_models", "-a", "-g", "-o", "--arrow-shape", "normal", "graph.png "]
subprocess.run(cmd)
Displays the name of the app where models.py
exists.
It's not a lot of code, but it's useful when checking when doing make migrations.
If you have a directory structure that says python manage.py startapp app
and you don't write a model, you need to delete models.py
.
import glob
def main():
model_files = glob.iglob('**/models.py', recursive=True)
for model_file in model_files:
path_split = model_file.split("/")
print(path_split[-2])
In addition to these, there are runserver_plus
that enhances django runserver, ʻadmin_generator` that automatically generates admin.py, etc., but they are not listed because I have never used them myself.
Recommended Posts