[PYTHON] [Django] A collection of scripts that are convenient for development

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.

environment

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)

Show URL

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)

Show TODO in source

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

Run the test

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)

Delete migration file

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}")

Database reset

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)

ER diagram generation

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)

App name display with models.py

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

[Django] A collection of scripts that are convenient for development
A summary of Python e-books that are useful for free-to-read data analysis
A collection of Numpy, Pandas Tips that are often used in the field
[Linux] A list of unique command selections that are convenient but unexpectedly unknown
A collection of resources that may be useful for creating and expanding dotfiles
Super simple: A collection of shells that output dates
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
A collection of one-liner web servers
Self-implementation of something like% timeit, a magic command of jupyter that is convenient for speed measurement, in Python
A memorandum of JSON Schema notation for dictionaries that take arbitrary keys
[Django] A collection of scripts that are convenient for development
A collection of methods used when aggregating data with pandas
Gold needle for when it becomes a stone by looking at the formula of image processing
A collection of tips for speeding up learning and reasoning with PyTorch
A site that helped me study Django
Create a model for your Django schedule
Commands for creating a new django project
Impressions of using Flask for a month
Build a TOP screen for Django users
Construction of development environment for Choreonoid class
Building a Python development environment for AI development
Creating a development environment for machine learning
Write a short if-else for Django Template
For beginners of SageMaker --Collection of material links -
A class for PYTHON that can be operated without being aware of LDAP
The story of Django creating a library that might be a little more useful
Let's display a simple template that is ideal for Django for the first time