liveBook · Manning

Deletion

As explained in Django in Action

DELETE is an HTTP method that requests removal of a resource identified by a URL.

DELETE is the HTTP method used by a REST-style API to request removal of a resource. In the CRUD shorthand, it supplies the delete verb: URLs identify the API’s nouns, while HTTP methods say what action to take on them. section 11.1.1 section 11.5

A delete request normally targets one object by putting its ID in the URL, such as /venue/{venue_id}/. The server looks up that object, removes it, and returns a response. If the ID is absent, the lookup produces a 404 response rather than quietly deleting nothing. section 11.2.7

Deleting a venue with Django Ninja

Register a route with @router.delete("/venue/{venue_id}/", auth=api_key). The handler uses get_object_or_404() to find the Venue, calls venue.delete(), then returns {"success": True}. The decorator defines the HTTP-facing DELETE endpoint; the model method performs the database removal. section 11.2.7 section 4.5

Listing 11.15 A delete endpoint for venues
# RiffMates/bands/api.py
...

@router.delete("/venue/{venue_id}/", auth=api_key)
def delete_venue(request, venue_id):
    venue = get_object_or_404(Venue, id=venue_id)
    venue.delete()

    return {"success": True}

This endpoint shows the full request path: URL ID, object lookup, database deletion, and a small success response. section 11.2.7

Choose DELETE according to the API’s purpose:

  • Provide it when clients such as mobile front ends need the full range of CRUD operations. section 11.2.7
  • Omit it from a read-only API, such as one intended only to expose data like an RSS feed. section 11.2.7
  • Return either the deleted object or success/failure metadata; returning the object can support undo or re-save behavior. section 11.2.7

Do not confuse an API DELETE request with Django’s .delete() method. DELETE is the HTTP instruction sent to an endpoint; calling .delete() on a retrieved model instance removes its corresponding database object. The Python instance can still exist in memory afterward, so inspect the database with a new query when verifying the result. section 4.5 section 4.12

How Django in Action covers it

How deep it goes
Works through a Django Ninja endpoint from URL ID lookup to model deletion and 404 behavior.
Examples
Demonstrates deleting a Venue through /venue/{venue_id}/ and deleting a Musician model instance.
What it assumes
Introduces DELETE through CRUD and HTTP while relying on Django models, routes, and object lookup conventions.
What you can do after
Build it from scratch as a Django Ninja route that locates, deletes, and reports on a resource.
How it connects
Connects CRUD, REST, HTTP, URLs, Django Ninja, get_object_or_404(), and Django model deletion.

Also explained in

Compare how 8 books explain deletion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.