Query Django#

This page explains the Django Internals helpers shipped by the Django CLI Package. With a few lines of code we will be able to get Django Instance, check database conenction, list registered applications and models, inspect models and create new ones.

👉 New to App-Generator? Sign IN with GitHub or Generate Web Apps in no time (free service).

Import CLI Package#

Download a starter that supports the Django CLI package: Datta Able (Free) or Dynamic Django (Paid) via Onboarding Kit Bundle. Once the project is downloaded, unzip it and inspect the files.

unzip django-datta-able.zip
cd django-datta-able
pip install -r requirements.txt

The CLI helpers are located in the cli package, root of the codebase:

Project Files#
< PROJECT_ROOT >
   |
   |-- cli/                       # CLI Package
        |-- __init__.py           # The entry point
        |-- common.py             # Constants
        |-- h_ai_claude.py        # Claude.AI Interface
        |-- h_code_parser.py      # AST-based helpers
        |-- h_django_common.py    # Manage Project dependencies
        |-- h_django_deps.py      # Manage Project dependencies
        |-- h_django_env.py       # Manage ENV
        |-- h_django_settings.py  # Manage Settings
        |-- h_django_urls.py      # Manage Routing
        |-- h_django.py           # DJANGO specific helpers
        |-- h_files.py            # Filesystem Helpers
        |-- h_git.py              # GIT Interface
        |-- h_shell.py            # Shell Interface
        |-- h_util.py             # Misc Helpers

After installing the dependencies, we can start the Python shell and import the CLI package in the Django or Python Shell.

Import CLI helpers#
# Usage via Django Shell
python manage.py shell
>>> from cli import *

# OR using the Python Shell
python                 # Start Python Shell
>>> from cli import *  # Import CLI Helpers
>>> h_random()         # Test the import by calling a helper
>>> 'Py8v76'

At this point, we can start using the CLI helpers.

Get Django Instance#

Using this helper, we can retrieve in the CLi an instance of Django.

get_django()#
>>> from cli import *
>>> get_django()
<django.apps.registry.Apps object at 0x000001BB8DE27A00>

Check DB Connection#

check_db_conn()#
>>> from cli import *
>>> check_db_conn()

Get Applications#

get_apps()#
>>> from cli import *
>>> get_apps()

Get App Models#

get_models(aApp)#
>>> from cli import *
>>> get_models(aApp)

Get Models Name#

get_models_name(aApp)#
>>> from cli import *
>>> get_models_name(aApp)

Get Model by Name#

get_model_by_name(aApp, aModelname)#
>>> from cli import *
>>> get_model_by_name(aApp, aModelname)

Get Model Fields#

get_model_fields(aModelClass)#
>>> from cli import *
>>> get_model_fields(aModelClass)

Get Model FKs#

get_model_fk(aModelClass)#
>>> from cli import *
>>> get_model_fk(aModelClass)

Check Model Migration#

check_model_migration(aModelClass)#
>>> from cli import *
>>> check_model_migration(aModelClass)

Extract Class Code#

extract_class_code(aFilePath, aClassName)#
>>> from cli import *
>>> extract_class_code(aFilePath, aClassName)

Add Model to App#

add_model(aAppName, aModelName)#
>>> from cli import *
>>> add_model(aAppName, aModelName)