Django (DRF)
Point appctl at a Django project that uses Django REST Framework. It reads your models and generates five tools per model.
Prerequisites
Section titled “Prerequisites”- A Django project folder. The parser looks for
manage.pyat the top andmodels.pyinside each app. rest_frameworkin yourINSTALLED_APPS. Without it the parser still runs, but the generated routes will not match anything real.- Python 3.11 or newer to run the demo.
appctlinstalled.
The demo in this repo
Section titled “The demo in this repo”examples/demos/django-drf/ is a Django 4.2 project with a billing app that has two models (Parcel and Customer). A DefaultRouter serves them at /api/parcels/ and /api/customers/.
1. Set up Python
Section titled “1. Set up Python”cd examples/demos/django-drfpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txt2. Create the database
Section titled “2. Create the database”The first run needs makemigrations because the initial migration for the billing app is generated from your models, not committed:
python manage.py makemigrations billingpython manage.py migrateOutput ends with:
Applying billing.0001_initial... OK3. Start the server
Section titled “3. Start the server”python manage.py runserver 127.0.0.1:8001Sanity check:
curl -s -X POST http://127.0.0.1:8001/api/parcels/ \ -H "Content-Type: application/json" \ -d '{"tracking_number":"PK-001","weight_kg":"1.5","delivered":false}'Output:
{"id":1,"tracking_number":"PK-001","weight_kg":"1.50","delivered":false}4. Sync appctl
Section titled “4. Sync appctl”Pass the path to the Django project folder, and a --base-url that includes your API mount prefix (/api in this demo):
appctl sync --django . --base-url http://127.0.0.1:8001/api --forceReal output with appctl 0.2.0:
Synced Django: 2 resources, 10 tools written to .appctlGenerated tools:
parcel: list_parcels, get_parcel, create_parcel, update_parcel, delete_parcelcustomer: list_customers, get_customer, create_customer, update_customer, delete_customer5. Talk to it
Section titled “5. Talk to it”appctl chat "how many parcels have been delivered"appctl chat "create a parcel with tracking PK-002 weighing 2kg"What appctl reads
Section titled “What appctl reads”manage.pyto confirm this is a Django project.- Any
settings.pyfile, to pick upINSTALLED_APPSand the root URL conf. */models.pyfor each installed app, to extract model names and field types.
It does not execute your Python code. If your models use dynamic class creation or a custom ModelMeta base, the parser will miss them.
Troubleshooting
Section titled “Troubleshooting”- Pass
/apiin--base-url. The sync writes tool paths as/parcels/, not/api/parcels/. If you pass the rawhttp://127.0.0.1:8001the tools will hit the wrong URL. Always include the prefix. appctl doctorneedsdjango_api_tokenset. Even though this demo usesAllowAny, the default Django auth strategy is a Bearer token. Exportdjango_api_token=unusedbefore running doctor.- Model migrations. The first sync after cloning needs
makemigrations billing && migratebefore the API returns anything.
Known limits
Section titled “Known limits”- Function based DRF views are not parsed. Only
ModelViewSetregistered on aDefaultRouter. - Custom
@actionmethods on a ViewSet are not extracted. - If your project publishes a Swagger document with
drf-spectacular, use--openapi; the output is better.