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
All the code for the example application is stored in ``app.py``.
112
+
.. step:: Create Your Application
112
113
113
-
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``:
114
+
All the code for the example application is stored in ``app.py``.
114
115
115
-
.. code-block:: python
116
+
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``:
"course": "Experiments, Science, and Fashion in Nanophotonics",
156
+
"gpa": 3.0,
157
+
}
158
+
},
159
+
)
160
+
161
+
This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.
162
+
163
+
I want to draw attention to the ``id`` field on this model. MongoDB uses ``_id``, but in Python, underscores at the start of attributes have special meaning. If you have an attribute on your model that starts with an underscore, `pydantic <https://pydantic-docs.helpmanual.io/>`__—the data validation framework used by FastAPI—will assume that it is a private variable, meaning you will not be able to assign it a value! To get around this, we name the field ``id`` but give it an alias of ``_id``. You also need to set ``populate_by_name`` to ``True`` in the model's ``model_config``
124
164
125
-
.. code-block:: python
165
+
We set this ``id`` value automatically to ``None``, so you do not need to supply it when creating a new student.
126
166
127
-
# Represents an ObjectId field in the database.
128
-
# It will be represented as a `str` on the model so that it can be
129
-
serialized to JSON.
130
-
PyObjectId = Annotated[str, BeforeValidator(str)]
167
+
.. code-block:: python
131
168
132
-
class StudentModel(BaseModel):
169
+
class UpdateStudentModel(BaseModel):
133
170
"""
134
-
Container for a single student record.
171
+
A set of optional updates to be made to a document in the database.
135
172
"""
136
173
137
-
# The primary key for the StudentModel, stored as a `str` on the instance.
138
-
# This will be aliased to ``_id`` when sent to MongoDB,
139
-
# but provided as ``id`` in the API requests and responses.
This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.
159
-
160
-
I want to draw attention to the ``id`` field on this model. MongoDB uses ``_id``, but in Python, underscores at the start of attributes have special meaning. If you have an attribute on your model that starts with an underscore, `pydantic <https://pydantic-docs.helpmanual.io/>`__—the data validation framework used by FastAPI—will assume that it is a private variable, meaning you will not be able to assign it a value! To get around this, we name the field ``id`` but give it an alias of ``_id``. You also need to set ``populate_by_name`` to ``True`` in the model's ``model_config``
161
-
162
-
We set this ``id`` value automatically to ``None``, so you do not need to supply it when creating a new student.
163
-
164
-
.. code-block:: python
165
-
166
-
class UpdateStudentModel(BaseModel):
167
-
"""
168
-
A set of optional updates to be made to a document in the database.
"course": "Experiments, Science, and Fashion in Nanophotonics",
183
-
"gpa": 3.0,
184
-
}
185
-
},
186
-
)
187
-
188
-
The ``UpdateStudentModel`` has two key differences from the ``StudentModel``:
189
-
190
-
- It does not have an ``id`` attribute as this cannot be modified.
191
-
- All fields are optional, so you only need to supply the fields you wish to update.
192
-
193
-
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.
194
-
195
-
.. code-block:: python
196
-
197
-
class StudentCollection(BaseModel):
198
-
"""
199
-
A container holding a list of `StudentModel` instances.
200
-
201
-
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/>`__
202
-
"""
203
-
204
-
students: List[StudentModel]
191
+
The ``UpdateStudentModel`` has two key differences from the ``StudentModel``:
192
+
193
+
- It does not have an ``id`` attribute as this cannot be modified.
194
+
- All fields are optional, so you only need to supply the fields you wish to update.
195
+
196
+
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.
197
+
198
+
.. code-block:: python
199
+
200
+
class StudentCollection(BaseModel):
201
+
"""
202
+
A container holding a list of `StudentModel` instances.
203
+
204
+
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/>`__
0 commit comments