bhylu.com

Advertisements

Django Deleting Records

Deletion is a state change. In a website it belongs behind a checked non-GET request and appropriate permissions, not a link that deletes when visited.

How it works

This standalone lesson creates and deletes a disposable venue in its private database. A queryset delete returns the total deleted-object count and a per-model breakdown. That total may include cascaded rows, so it is not always equal to the number of parent records selected. Restrict the target precisely and understand the relationship rules before writing any destructive operation.

Run in the supplied local Django project

This lesson uses Django 5.2.18 LTS with Python 3.12. Download the source downloader and run node download-project.mjs http://127.0.0.1:8000 with Node 22 while previewing locally. It creates a new bhylu-django-lab folder and refuses to overwrite an existing one. After deployment, the default base is bhylu.com; until then use your local preview URL.

Inside that new folder, create a virtual environment, install requirements.txt and run the complete lesson file shown below. Every lesson creates a fresh in-memory SQLite database and temporary upload storage. It does not edit the interactive lab’s db.sqlite3 or Bhylu’s database. The visible listing below is the lesson’s core code; its complete downloaded file includes the shared bootstrap.

Django does not run in Bhylu’s current browser playground. No Apache, MySQL or global PHP configuration change is needed. See the setup lesson for the full project and commands.

Example

Run .venv\Scripts\python.exe lessons\delete-records.py from the downloaded project root. Download the complete lesson file; keep it under lessons/. The project’s shared models, forms, views and fixture are prerequisites, not a hidden online runtime.

lessons/delete-records.py — core code

venue = Venue.objects.create(code="temporary-room", name="Temporary Room")
identifier = venue.pk
count, breakdown = Venue.objects.filter(pk=identifier).delete()
print(count)
print(Venue.objects.filter(pk=identifier).exists())
print(Workshop.objects.count())

Expected result

1
False
3

Only the new unused venue is removed; the three seeded workshops remain. The test uses the created primary key rather than a broad title filter and checks the resulting state explicitly.

Use Django’s versioned official documentation for exact API contracts and the deployment checklist before a separate release.

Advertisements