|
| 1 | +Build a Simple API(CRUD) |
| 2 | +================= |
| 3 | + |
| 4 | +This example demonstrates how to build a simple CRUD (Create, Read, Update, Delete) |
| 5 | +API using NanoDjango’s built-in `Django Ninja <https://django-ninja.dev/>`_ support. |
| 6 | + |
| 7 | +We implement a minimal in-memory "Todo" database with endpoints for creating, |
| 8 | +listing, retrieving, updating, and deleting items. |
| 9 | + |
| 10 | +Example code |
| 11 | +------------ |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + # hello_api.py |
| 16 | + from nanodjango.app import Django |
| 17 | +
|
| 18 | + # Initialise NanoDjango |
| 19 | + app = Django() |
| 20 | +
|
| 21 | + # Fake in-memory database |
| 22 | + todos = {} |
| 23 | +
|
| 24 | + # ---- Schema for Todo ---- |
| 25 | + class Todo(app.ninja.Schema): |
| 26 | + id: int | None = None # Auto-assigned |
| 27 | + title: str |
| 28 | + done: bool = False |
| 29 | +
|
| 30 | + # ---- CREATE ---- |
| 31 | + @app.api.post("/todos") |
| 32 | + def create_todo(request, payload: Todo): |
| 33 | + todo_id = len(todos) + 1 |
| 34 | + todo = Todo(id=todo_id, title=payload.title, done=payload.done) |
| 35 | + todos[todo_id] = todo.dict() |
| 36 | + return todos[todo_id] |
| 37 | +
|
| 38 | + # ---- READ ALL ---- |
| 39 | + @app.api.get("/todos") |
| 40 | + def list_todos(request): |
| 41 | + return list(todos.values()) |
| 42 | +
|
| 43 | + # ---- READ ONE ---- |
| 44 | + @app.api.get("/todos/{todo_id}") |
| 45 | + def get_todo(request, todo_id: int): |
| 46 | + todo = todos.get(todo_id) |
| 47 | + if not todo: |
| 48 | + return {"error": "Todo not found"}, 404 |
| 49 | + return todo |
| 50 | +
|
| 51 | + # ---- UPDATE ---- |
| 52 | + @app.api.put("/todos/{todo_id}") |
| 53 | + def update_todo(request, todo_id: int, payload: Todo): |
| 54 | + if todo_id not in todos: |
| 55 | + return {"error": "Todo not found"}, 404 |
| 56 | + updated = todos[todo_id] |
| 57 | + updated["title"] = payload.title |
| 58 | + updated["done"] = payload.done |
| 59 | + todos[todo_id] = updated |
| 60 | + return updated |
| 61 | +
|
| 62 | + # ---- DELETE ---- |
| 63 | + @app.api.delete("/todos/{todo_id}") |
| 64 | + def delete_todo(request, todo_id: int): |
| 65 | + if todo_id not in todos: |
| 66 | + return {"error": "Todo not found"}, 404 |
| 67 | + del todos[todo_id] |
| 68 | + return {"message": "Deleted"} |
| 69 | +
|
| 70 | +
|
| 71 | +Running the app |
| 72 | +--------------- |
| 73 | + |
| 74 | +Run the API using : |
| 75 | + |
| 76 | +.. code-block:: bash |
| 77 | +
|
| 78 | + cd /examples |
| 79 | + python -m nanodjango run hello_api.py |
| 80 | +
|
| 81 | +
|
| 82 | +Endpoints |
| 83 | +--------- |
| 84 | + |
| 85 | +All endpoints are available under ``/api/``: |
| 86 | + |
| 87 | +**1. Create a Todo (POST)** |
| 88 | + |
| 89 | +- URL: ``/api/todos`` |
| 90 | +- Body: JSON object with ``title`` and optional ``done`` |
| 91 | + |
| 92 | +.. code-block:: bash |
| 93 | +
|
| 94 | + curl -X POST http://127.0.0.1:8000/api/todos \ |
| 95 | + -H "Content-Type: application/json" \ |
| 96 | + -d '{"title": "Learn NanoDjango", "done": false}' |
| 97 | +
|
| 98 | +Response: |
| 99 | + |
| 100 | +.. code-block:: json |
| 101 | +
|
| 102 | + {"id": 1, "title": "Learn NanoDjango", "done": false} |
| 103 | +
|
| 104 | +
|
| 105 | +**2. List all Todos (GET)** |
| 106 | + |
| 107 | +- URL: ``/api/todos`` |
| 108 | + |
| 109 | +.. code-block:: bash |
| 110 | +
|
| 111 | + curl http://127.0.0.1:8000/api/todos |
| 112 | +
|
| 113 | +Response: |
| 114 | + |
| 115 | +.. code-block:: json |
| 116 | +
|
| 117 | + [{"id": 1, "title": "Learn NanoDjango", "done": false}] |
| 118 | +
|
| 119 | +
|
| 120 | +**3. Get a Todo by ID (GET)** |
| 121 | + |
| 122 | +- URL: ``/api/todos/{id}`` |
| 123 | + |
| 124 | +.. code-block:: bash |
| 125 | +
|
| 126 | + curl http://127.0.0.1:8000/api/todos/1 |
| 127 | +
|
| 128 | +Response: |
| 129 | + |
| 130 | +.. code-block:: json |
| 131 | +
|
| 132 | + {"id": 1, "title": "Learn NanoDjango", "done": false} |
| 133 | +
|
| 134 | +
|
| 135 | +**4. Update a Todo (PUT)** |
| 136 | + |
| 137 | +- URL: ``/api/todos/{id}`` |
| 138 | +- Body: JSON object with updated ``title`` and ``done`` |
| 139 | + |
| 140 | +.. code-block:: bash |
| 141 | +
|
| 142 | + curl -X PUT http://127.0.0.1:8000/api/todos/1 \ |
| 143 | + -H "Content-Type: application/json" \ |
| 144 | + -d '{"title": "Learn N@n0Dj@n60 deeply", "done": true}' |
| 145 | +
|
| 146 | +Response: |
| 147 | + |
| 148 | +.. code-block:: json |
| 149 | +
|
| 150 | + {"id": 1, "title": "Learn N@n0Dj@n60 deeply", "done": true} |
| 151 | +
|
| 152 | +
|
| 153 | +**5. Delete a Todo (DELETE)** |
| 154 | + |
| 155 | +- URL: ``/api/todos/{id}`` |
| 156 | + |
| 157 | +.. code-block:: bash |
| 158 | +
|
| 159 | + curl -X DELETE http://127.0.0.1:8000/api/todos/1 |
| 160 | +
|
| 161 | +Response: |
| 162 | + |
| 163 | +.. code-block:: json |
| 164 | +
|
| 165 | + {"message": "Deleted"} |
| 166 | +
|
| 167 | +
|
| 168 | +Notes |
| 169 | +----- |
| 170 | + |
| 171 | +- ``@app.api`` is an instance of ``NinjaAPI`` automatically mounted at ``/api/``. |
| 172 | +- ``app.ninja.Schema`` is used to define input validation and output schema. |
| 173 | +- This example stores data in an in-memory dictionary, which resets on restart. |
| 174 | +- For persistence, you can later integrate with Django ORM models. |
0 commit comments