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
+72-50Lines changed: 72 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
.. _pymongo-fastapi:
2
2
.. original URL: https://www.mongodb.com/developer/languages/python/python-quickstart-fastapi/
3
3
4
-
============================
5
-
FastAPI Integration Tutorial
6
-
============================
4
+
=============================
5
+
Tutorial: FastAPI Integration
6
+
=============================
7
7
8
8
.. contents:: On this page
9
9
:local:
@@ -16,92 +16,105 @@ FastAPI Integration Tutorial
16
16
:values: tutorial
17
17
18
18
.. meta::
19
-
:description: Learn how to create an app to connect to MongoDB deployment by using the PyMongo driver.
19
+
:description: Build an application use FastAPI that integrates with a MongoDB deployment by using the PyMongo Async driver.
20
20
:keywords: quick start, tutorial, basics
21
21
22
22
Overview
23
23
--------
24
24
25
25
`FastAPI <https://fastapi.tiangolo.com/>`__ is a high-performance,
26
-
production-ready asynchronous Python framework for building APIs based on
27
-
standard Python type hints. In this tutorial, we will create a CRUD application
28
-
showing how you can integrate MongoDB with your FastAPI projects.
26
+
production-ready asynchronous {+language+} framework for building APIs based on
27
+
standard {+language+} type hints. In this tutorial, you can learn how to create a CRUD application
28
+
that integrates MongoDB with your FastAPI projects.
29
29
30
30
Tutorial
31
31
--------
32
32
33
-
This tutorial walks through the `MongoDB with FastAPI sample project <https://github.com/mongodb-developer/mongodb-with-fastapi>`__.
33
+
You can find the completed sample app for this tutorial in the :github:`MongoDB
34
+
with FastAPI sample project
35
+
repository</mongodb-developer/mongodb-with-fastapi>`__ on GitHub.
34
36
35
37
Prerequisites
36
38
~~~~~~~~~~~~~
37
39
38
-
- Python v3.9.0 or later
39
-
- A MongoDB Atlas cluster
40
-
See the `Get Started with Atlas
41
-
<https://docs.atlas.mongodb.com/getting-started/>`__ guide to create your
42
-
account and MongoDB cluster.
40
+
- Python v3.9.0 or later
41
+
- A MongoDB Atlas cluster
42
+
See the :re:`Getting Started <pymongo-get-started>`__ guide for more information.
43
43
44
44
Set-up
45
45
~~~~~~
46
46
47
47
.. procedure::
48
48
:style: connected
49
49
50
-
.. step:: Clone the example code from the `mongodb-with-fastapi repository <https://github.com/mongodb-developer/mongodb-with-fastapi>`__:
50
+
.. step:: Clone the example code example
51
+
52
+
Run the following command in your terminal to clone the code from the `mongodb-with-fastapi repository <https://github.com/mongodb-developer/mongodb-with-fastapi>`__:
.. step:: Install the required dependencies listed in the ``requirements.txt`` file:
58
+
.. step:: Install the required dependencies.
59
+
60
+
.. tip:: Use a Virtual environment
57
61
58
-
.. tip:: Use a Virtual environment
62
+
Installing your Python dependencies in a `virtualenv <https://docs.python.org/3/tutorial/venv.html>`__ with allow for versions of the libraries to be install for individual projects. Before running pip, ensure your ``virtualenv`` is active.
63
+
64
+
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
59
65
60
-
Installing your Python dependencies in a `virtualenv <https://docs.python.org/3/tutorial/venv.html>`__ with allow for versions of the libraries to be install for individual projects. Before running pip, ensure your ``virtualenv`` is active.
61
-
62
-
.. code-block:: shell
63
-
64
-
cd mongodb-with-fastapi
65
-
pip install -r requirements.txt
66
-
67
-
It may take a few moments to download and install your dependencies.
68
-
69
-
.. step:: Create an environment variable for your MongoDB connection string:
It may take a few moments to download and install your dependencies.
76
72
77
-
Anytime you start a new terminal session, you will must reset this
78
-
environment variable. You can use `direnv <https://direnv.net/>`__ to make
79
-
this process easier.
73
+
.. step:: Retrieve your connection string
74
+
75
+
Follow the :manual:`Find Your MongoDB Atlas Connection String guide </reference/connection-string/#find-your-mongodb-atlas-connection-string>` to retrieve your connection string.
76
+
77
+
Run the following code in your terminal to create an environment variable to store your connection string:
80
78
81
-
.. step:: Start your FastAPI server:
79
+
..
80
+
is this necessary? Are we connecting to MongoDB in this step?
Connect to your MongoDB Atlas cluster using the asynchronous `Pymongo Driver <https://www.mongodb.com/docs/languages/python/pymongo-driver/current>`__, then specify the database named ``college``:
111
+
Use the following code to :ref:`connect to your MongoDB Atlas cluster
112
+
<pymongo_connect_atlas>` by using the {+driver-async+} ``AsyncMongoClient()``
113
+
method and the ``MONGODB_URL`` environment variable, then specify the database named ``college``:
Our application has three models, the ``StudentModel``, the ``UpdateStudentModel``, and the ``StudentCollection``.
135
+
Our application has three models, the ``StudentModel``, the
136
+
``UpdateStudentModel``, and the ``StudentCollection``.
137
+
138
+
Define the ``StudentModel`` class using the following code:
123
139
124
140
.. code-block:: python
125
141
@@ -154,7 +170,9 @@ Our application has three models, the ``StudentModel``, the ``UpdateStudentModel
154
170
},
155
171
)
156
172
157
-
This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.
173
+
This is the primary model we use as the `response model
174
+
<https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of
175
+
our endpoints.
158
176
159
177
I want to draw attention to the ``id`` field on this model. MongoDB uses
160
178
``_id``, but in Python, underscores at the start of attributes have special
@@ -168,6 +186,8 @@ cannot assign it a value. To get around this, we name the field
168
186
We set this ``id`` value automatically to ``None``, so that can create a new
169
187
student with out specifying it.
170
188
189
+
Define the ``UpdateStudentModel`` class using the following code:
190
+
171
191
.. code-block:: python
172
192
173
193
class UpdateStudentModel(BaseModel):
@@ -199,6 +219,8 @@ The ``UpdateStudentModel`` has two key differences from the ``StudentModel``:
199
219
200
220
Finally, ``StudentCollection`` is defined to encapsulate a list of ``StudentModel`` instances. In theory, the endpoint could return a top-level list of StudentModels, but there are some vulnerabilities associated with returning JSON responses with top-level lists.
201
221
222
+
Define the ``StudentCollection`` class using the following code:
0 commit comments