@@ -312,26 +312,26 @@ You can see the ``create_student`` definition in the following code in the ``app
312
312
313
313
.. code-block:: python
314
314
315
- @app.post(
316
- "/students/",
317
- response_description="Add new student",
318
- response_model=StudentModel,
319
- status_code=status.HTTP_201_CREATED,
320
- response_model_by_alias=False,
321
- )
322
- async def create_student(student: StudentModel = Body(...)):
323
- """
324
- Insert a new student record.
325
-
326
- A unique ``id`` will be created and provided in the response.
327
- """
328
- new_student = await student_collection.insert_one(
329
- student.model_dump(by_alias=True, exclude=["id"])
330
- )
331
- created_student = await student_collection.find_one(
332
- {"_id": new_student.inserted_id}
333
- )
334
- return created_student
315
+ @app.post(
316
+ "/students/",
317
+ response_description="Add new student",
318
+ response_model=StudentModel,
319
+ status_code=status.HTTP_201_CREATED,
320
+ response_model_by_alias=False,
321
+ )
322
+ async def create_student(student: StudentModel = Body(...)):
323
+ """
324
+ Insert a new student record.
325
+
326
+ A unique ``id`` will be created and provided in the response.
327
+ """
328
+ new_student = await student_collection.insert_one(
329
+ student.model_dump(by_alias=True, exclude=["id"])
330
+ )
331
+ created_student = await student_collection.find_one(
332
+ {"_id": new_student.inserted_id}
333
+ )
334
+ return created_student
335
335
336
336
Read Routes
337
337
```````````
@@ -343,20 +343,20 @@ You can see the ``list_students`` definition in the following code in the ``app.
343
343
344
344
.. code-block:: python
345
345
346
- @app.get(
347
- "/students/",
348
- response_description="List all students",
349
- response_model=StudentCollection,
350
- response_model_by_alias=False,
351
- )
352
- async def list_students():
353
- """
354
- List all the student data in the database.
346
+ @app.get(
347
+ "/students/",
348
+ response_description="List all students",
349
+ response_model=StudentCollection,
350
+ response_model_by_alias=False,
351
+ )
352
+ async def list_students():
353
+ """
354
+ List all the student data in the database.
355
355
356
- The response is unpaginated and limited to 1000 results.
357
- """
356
+ The response is unpaginated and limited to 1000 results.
357
+ """
358
358
359
- return StudentCollection(students=await student_collection.find().to_list(1000))
359
+ return StudentCollection(students=await student_collection.find().to_list(1000))
360
360
361
361
.. note:: Results Pagination
362
362
@@ -376,22 +376,22 @@ You can see the ``show_students`` definition in the following code in the ``app.
376
376
377
377
.. code-block:: python
378
378
379
- @app.get(
380
- "/students/{id}",
381
- response_description="Get a single student",
382
- response_model=StudentModel,
383
- response_model_by_alias=False,
384
- )
385
- async def show_student(id: str):
386
- """
387
- Get the record for a specific student, looked up by ``id``.
388
- """
389
- if (
390
- student := await student_collection.find_one({"_id": ObjectId(id)})
391
- ) is not None:
392
- return student
379
+ @app.get(
380
+ "/students/{id}",
381
+ response_description="Get a single student",
382
+ response_model=StudentModel,
383
+ response_model_by_alias=False,
384
+ )
385
+ async def show_student(id: str):
386
+ """
387
+ Get the record for a specific student, looked up by ``id``.
388
+ """
389
+ if (
390
+ student := await student_collection.find_one({"_id": ObjectId(id)})
391
+ ) is not None:
392
+ return student
393
393
394
- raise HTTPException(status_code=404, detail="Student {id} not found")
394
+ raise HTTPException(status_code=404, detail="Student {id} not found")
395
395
396
396
Update Route
397
397
`````````````
@@ -523,7 +523,7 @@ Now that you have a basic understanding of how FastAPI integrates with MongoDB a
523
523
- `Dependency injection <https://fastapi.tiangolo.com/tutorial/background-tasks/#dependency-injection>`__
524
524
- `Testing with TestClient <https://fastapi.tiangolo.com/reference/testclient/>`__
525
525
- `Error handling and custom responses <https://fastapi.tiangolo.com/tutorial/handling-errors/>`__
526
- - `Authentication and authorization <https://fastapi.tiangolo.com/tutorial/security/>`__`
526
+ - `Authentication and authorization <https://fastapi.tiangolo.com/tutorial/security/>`__
527
527
528
528
More Resources
529
529
--------------
0 commit comments