@@ -312,26 +312,26 @@ You can see the ``create_student`` definition in the following code in the ``app
312312
313313.. code-block:: python
314314
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
335335
336336Read Routes
337337```````````
@@ -343,20 +343,20 @@ You can see the ``list_students`` definition in the following code in the ``app.
343343
344344.. code-block:: python
345345
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.
355355
356- The response is unpaginated and limited to 1000 results.
357- """
356+ The response is unpaginated and limited to 1000 results.
357+ """
358358
359- return StudentCollection(students=await student_collection.find().to_list(1000))
359+ return StudentCollection(students=await student_collection.find().to_list(1000))
360360
361361.. note:: Results Pagination
362362
@@ -376,22 +376,22 @@ You can see the ``show_students`` definition in the following code in the ``app.
376376
377377.. code-block:: python
378378
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
393393
394- raise HTTPException(status_code=404, detail="Student {id} not found")
394+ raise HTTPException(status_code=404, detail="Student {id} not found")
395395
396396Update Route
397397`````````````
@@ -523,7 +523,7 @@ Now that you have a basic understanding of how FastAPI integrates with MongoDB a
523523- `Dependency injection <https://fastapi.tiangolo.com/tutorial/background-tasks/#dependency-injection>`__
524524- `Testing with TestClient <https://fastapi.tiangolo.com/reference/testclient/>`__
525525- `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/>`__
527527
528528More Resources
529529--------------
0 commit comments