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
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",
163
-
"gpa": 3.0,
164
-
}
165
-
},
166
-
)
167
-
168
-
This is the primary model we use as the `response model <https://fastapi.tiangolo.com/tutorial/response-model/>`__ for the majority of our endpoints.
169
-
170
-
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``
171
-
172
-
We set this ``id`` value automatically to ``None``, so you do not need to supply it when creating a new student.
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)]
131
+
132
+
class StudentModel(BaseModel):
133
+
"""
134
+
Container for a single student record.
135
+
"""
136
+
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.
"course": "Experiments, Science, and Fashion in Nanophotonics",
153
+
"gpa": 3.0,
154
+
}
155
+
},
156
+
)
157
+
158
+
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.
173
163
174
-
.. code-block:: python
164
+
.. code-block:: python
175
165
176
-
class UpdateStudentModel(BaseModel):
177
-
"""
178
-
A set of optional updates to be made to a document in the database.
"course": "Experiments, Science, and Fashion in Nanophotonics",
193
-
"gpa": 3.0,
194
-
}
195
-
},
196
-
)
197
-
198
-
The ``UpdateStudentModel`` has two key differences from the ``StudentModel``:
199
-
200
-
- It does not have an ``id`` attribute as this cannot be modified.
201
-
- All fields are optional, so you only need to supply the fields you wish to update.
202
-
203
-
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.
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.
204
194
205
-
.. code-block:: python
195
+
.. code-block:: python
206
196
207
-
class StudentCollection(BaseModel):
208
-
"""
209
-
A container holding a list of `StudentModel` instances.
197
+
class StudentCollection(BaseModel):
198
+
"""
199
+
A container holding a list of `StudentModel` instances.
210
200
211
-
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/>`__
212
-
"""
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/>`__
0 commit comments