Skip to content

Commit 2137503

Browse files
committed
resources
1 parent 8ef3d18 commit 2137503

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

source/integrations/fastapi-integration.txt

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Prerequisites
5656
<https://docs.atlas.mongodb.com/getting-started/>`__ guide to create your
5757
account and MongoDB cluster.
5858

59-
1. Set-up
60-
~~~~~~~~~
59+
Set-up
60+
~~~~~~
6161

6262
.. procedure::
6363
:style: connected
@@ -107,8 +107,8 @@ Prerequisites
107107
.. image:: /includes/integrations/fastapi-browser.png
108108
:alt: Screenshot of browser and swagger UI
109109

110-
#. Create Your Application
111-
~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
Create Your Application
111+
~~~~~~~~~~~~~~~~~~~~~~~
112112

113113
All the code for the example application is stored in ``app.py``.
114114

@@ -120,8 +120,8 @@ Connect to your MongoDB Atlas cluster using the asynchronous `Pymongo Driver <ht
120120
db = client.get_database("college")
121121
student_collection = db.get_collection("students")
122122

123-
#. Define Your Database Models
124-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123+
Define Your Database Models
124+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
125125

126126
Our application has three models, the ``StudentModel``, the ``UpdateStudentModel``, and the ``StudentCollection``.
127127

@@ -206,8 +206,8 @@ Finally, ``StudentCollection`` is defined to encapsulate a list of ``StudentMode
206206

207207
students: List[StudentModel]
208208

209-
#. Create Your Application Routes
210-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209+
Create Your Application Routes
210+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211211

212212
Our application has five routes:
213213

@@ -268,7 +268,7 @@ Our application has five routes:
268268

269269
FastAPI returns an HTTP ``200`` status code by default; but in this instance, a ``201`` created is more appropriate.
270270

271-
.. step:: Read Routes
271+
.. step:: Create Read Routes
272272

273273
The application has two read routes: one for viewing all students, and one for viewing an individual student.
274274

@@ -313,7 +313,7 @@ Our application has five routes:
313313

314314
If a document with the specified ``_id`` does not exist, we raise an ``HTTPException`` with a status of ``404``.
315315

316-
.. step:: Update Route
316+
.. step:: Create Update Route
317317

318318
.. code-block:: python
319319

@@ -357,38 +357,33 @@ Our application has five routes:
357357

358358
If we get to the end of the function and we have not been able to find a matching document to update or return, then we raise a ``404`` error again.
359359

360-
Delete Route
361-
++++++++++++
362-
363-
.. code-block:: python
364-
365-
@app.delete("/students/{id}", response_description="Delete a student")
366-
async def delete_student(id: str):
367-
"""
368-
Remove a single student record from the database.
369-
"""
370-
delete_result = await student_collection.delete_one({"_id": ObjectId(id)})
360+
.. step:: Create Delete Route
371361

372-
if delete_result.deleted_count == 1:
373-
return Response(status_code=status.HTTP_204_NO_CONTENT)
362+
.. code-block:: python
374363

375-
raise HTTPException(status_code=404, detail=f"Student {id} not found")
364+
@app.delete("/students/{id}", response_description="Delete a student")
365+
async def delete_student(id: str):
366+
"""
367+
Remove a single student record from the database.
368+
"""
369+
delete_result = await student_collection.delete_one({"_id": ObjectId(id)})
376370

377-
Our final route is ``delete_student``. Again, because this is acting upon a single document, we have to supply an ``id`` in the URL. If we find a matching document and successfully delete it, then we return an HTTP status of ``204`` or "No Content." In this case, we do not return a document as we've already deleted it! However, if we cannot find a student with the specified ``id``, then instead we return a ``404``.
371+
if delete_result.deleted_count == 1:
372+
return Response(status_code=status.HTTP_204_NO_CONTENT)
378373

379-
Our New FastAPI App Generator
380-
-----------------------------
374+
raise HTTPException(status_code=404, detail=f"Student {id} not found")
381375

382-
If you're excited to build something more production-ready with FastAPI, React &
383-
MongoDB, head over to the `full-stack-fastapi-mongodb Github repository
384-
<https://github.com/mongodb-labs/full-stack-fastapi-mongodb>`__ for our `new
385-
FastAPI app generator
386-
<https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__
387-
and start transforming your web development experience.
376+
Our final route is ``delete_student``. Again, because this is acting upon a single document, we have to supply an ``id`` in the URL. If we find a matching document and successfully delete it, then we return an HTTP status of ``204`` or "No Content." In this case, we do not return a document as we've already deleted it! However, if we cannot find a student with the specified ``id``, then instead we return a ``404``.
388377

389-
Wrapping Up
390-
-----------
378+
More Resources
379+
--------------
391380

392-
I hope you have found this introduction to FastAPI with MongoDB useful. If you would like to learn more, check out my post `introducing the FARM stack (FastAPI, React and MongoDB) <https://developer.mongodb.com/how-to/FARM-Stack-FastAPI-React-MongoDB>`__ as well as the `FastAPI documentation <https://motor.readthedocs.io>`__ and this `awesome list <https://github.com/mjhea0/awesome-fastapi>`__.
381+
For more information, see the following resources:
393382

394-
If you have questions, please head to our `developer community website <https://community.mongodb.com/>`__ where MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.
383+
- Full Stack FastAPI App Generator
384+
- `Blog post <https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__
385+
- `Github repository <https://github.com/mongodb-labs/full-stack-fastapi-mongodb>`__
386+
- `Introducing the FARM stack (FastAPI, React and MongoDB) blog post <https://developer.mongodb.com/how-to/FARM-Stack-FastAPI-React-MongoDB>`__
387+
- `FastAPI documentation <https://motor.readthedocs.io>`__
388+
- `List of FastAPI Integration Options <https://github.com/mjhea0/awesome-fastapi>`__
389+
- `MongoDB Developer Community <https://community.mongodb.com/>`__

0 commit comments

Comments
 (0)