You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/integrations/fastapi-integration.txt
+62-54Lines changed: 62 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -39,10 +39,10 @@ Prerequisites
39
39
Ensure you have the following components installed and set up before you start
40
40
this tutorial:
41
41
42
-
- Python v3.9.0 or later
43
-
- A MongoDB Atlas cluster
42
+
- Python v3.9.0 or later.
43
+
- A MongoDB Atlas cluster.
44
44
To learn how to set up a cluster, see
45
-
the :ref:`Getting Started <pymongo-get-started>`__ guide for more information.
45
+
the :ref:`Getting Started <pymongo-get-started>` guide for more information.
46
46
47
47
Set-up
48
48
~~~~~~
@@ -54,69 +54,74 @@ Install dependencies, connect to MongoDB and start your FastAPI server.
54
54
55
55
.. step:: Clone the example code example.
56
56
57
-
Run the following command in your terminal to clone the code from the `mongodb-with-fastapi <https://github.com/mongodb-developer/mongodb-with-fastapi>`__ GitHub repository:
57
+
Run the following command in your terminal to clone the code from the
Installing your Python dependencies in a `virtualenv
68
-
<https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
69
-
versions of your libraries for individual
70
-
projects. Before running any ``pip`` commands, ensure your ``virtualenv`` is active.
69
+
Installing your Python dependencies in a `virtualenv
70
+
<https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
71
+
versions of your libraries for individual projects. Before running any
72
+
``pip`` commands, ensure your ``virtualenv`` is active.
71
73
72
-
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
74
+
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
73
75
74
-
.. code-block:: shell
76
+
.. code-block:: shell
75
77
76
-
cd mongodb-with-fastapi
77
-
pip install -r requirements.txt
78
+
cd mongodb-with-fastapi
79
+
pip install -r requirements.txt
78
80
79
-
It might take a few moments to download and install your dependencies.
81
+
It might take a few moments to download and install your dependencies.
80
82
81
83
.. step:: Retrieve your connection string.
82
-
83
-
Follow the :manual:`Find Your MongoDB Atlas Connection String guide </reference/connection-string/#find-your-mongodb-atlas-connection-string>` to retrieve your connection string.
84
-
85
-
Run the following code in your terminal to create an environment variable to store your connection string:
86
84
87
-
.. code-block:: shell
85
+
Follow the :manual:`Find Your MongoDB Atlas Connection String guide
Define the ``StudentCollection`` class by adding the following code:
245
253
246
254
.. code-block:: python
247
255
248
256
class StudentCollection(BaseModel):
249
257
"""
250
-
A container holding a list of `StudentModel` instances.
251
-
252
-
This exists because providing a top-level array in a JSON response can be a `vulnerability <https://haacked.com/archive/2009/06/25/json-hijacking.aspx/>`__
258
+
A container holding a list of `StudentModel` instances
253
259
"""
254
260
255
261
students: List[StudentModel]
@@ -283,6 +289,8 @@ with your data:
283
289
* - ``DELETE /students/{id}``
284
290
- Delete a student
285
291
292
+
All of these routes are defined in the ``app.py`` file.
293
+
286
294
.. procedure::
287
295
:style: connected
288
296
@@ -328,8 +336,8 @@ with your data:
328
336
329
337
.. step:: Create the Read routes.
330
338
331
-
This application has two read routes: one for viewing all students, and one
332
-
for viewing an individual student specified by their ``id``.
339
+
This application one route for viewing all students, and one
340
+
for viewing an individual student, specified by their ``id``.
333
341
334
342
Define the ``list_students`` route to view all students by adding the following code:
335
343
@@ -354,7 +362,7 @@ with your data:
354
362
355
363
This example uses the ``to_list()`` method; but in a real application,
The student detail route has a path parameter of ``id``, which FastAPI
@@ -387,7 +395,7 @@ with your data:
387
395
388
396
.. step:: Create the Update route.
389
397
390
-
The ``update_student`` route is like a combination of the
398
+
The ``update_student`` route functions similar to a combination of the
391
399
``create_student`` and the ``show_student`` routes. It receives the ``id``
392
400
of the student to update, and the new data in the JSON body.
393
401
@@ -472,9 +480,9 @@ For more information about FastAPI integration, see the following resources:
472
480
473
481
- MongoDB's Full Stack FastAPI App Generator
474
482
- `Blog post: Introducing the Full Stack FastAPI App Generator for Python Developers <https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__
0 commit comments