Skip to content

Commit 9fdb2c0

Browse files
author
Lev Rubel
committed
added sentinel value to allow None values in id to be passed in models internal methods (thus in pymongo)
1 parent 480b06e commit 9fdb2c0

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

HISTORY.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
History
33
=======
44

5+
0.2.5
6+
--------
7+
8+
Bugfix:
9+
10+
* Added sentinel value to allow None values in "id" field to be passed to Model internal methods
11+
512
0.2.4
613
--------
714

docs/fastapi_contrib.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ fastapi\_contrib.permissions module
5555
:undoc-members:
5656
:show-inheritance:
5757

58+
fastapi\_contrib.routes module
59+
------------------------------
60+
61+
.. automodule:: fastapi_contrib.routes
62+
:members:
63+
:undoc-members:
64+
:show-inheritance:
65+
5866

5967
Module contents
6068
---------------

fastapi_contrib/db/client.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pymongo.cursor import Cursor
55
from pymongo.results import InsertOneResult, DeleteResult, UpdateResult
66

7-
from fastapi_contrib.db.models import MongoDBModel
7+
from fastapi_contrib.db.models import MongoDBModel, notset
88
from fastapi_contrib.common.utils import get_current_app, get_timezone
99

1010

@@ -50,8 +50,8 @@ async def insert(
5050
async def count(
5151
self, model: MongoDBModel, session: ClientSession = None, **kwargs
5252
) -> int:
53-
_id = kwargs.pop("id", None)
54-
if _id is not None:
53+
_id = kwargs.pop("id", notset)
54+
if _id != notset:
5555
kwargs["_id"] = _id
5656

5757
collection_name = model.get_db_collection()
@@ -62,8 +62,8 @@ async def count(
6262
async def delete(
6363
self, model: MongoDBModel, session: ClientSession = None, **kwargs
6464
) -> DeleteResult:
65-
_id = kwargs.pop("id", None)
66-
if _id is not None:
65+
_id = kwargs.pop("id", notset)
66+
if _id != notset:
6767
kwargs["_id"] = _id
6868

6969
collection_name = model.get_db_collection()
@@ -78,8 +78,8 @@ async def update_one(
7878
session: ClientSession = None,
7979
**kwargs
8080
) -> UpdateResult:
81-
_id = filter_kwargs.pop("id", None)
82-
if _id is not None:
81+
_id = filter_kwargs.pop("id", notset)
82+
if _id != notset:
8383
filter_kwargs["_id"] = _id
8484

8585
collection_name = model.get_db_collection()
@@ -96,8 +96,8 @@ async def update_many(
9696
session: ClientSession = None,
9797
**kwargs
9898
) -> UpdateResult:
99-
_id = filter_kwargs.pop("id", None)
100-
if _id is not None:
99+
_id = filter_kwargs.pop("id", notset)
100+
if _id != notset:
101101
filter_kwargs["_id"] = _id
102102

103103
collection_name = model.get_db_collection()
@@ -110,8 +110,8 @@ async def update_many(
110110
async def get(
111111
self, model: MongoDBModel, session: ClientSession = None, **kwargs
112112
) -> dict:
113-
_id = kwargs.pop("id", None)
114-
if _id is not None:
113+
_id = kwargs.pop("id", notset)
114+
if _id != notset:
115115
kwargs["_id"] = _id
116116

117117
collection_name = model.get_db_collection()
@@ -128,8 +128,8 @@ def list(
128128
_sort: list = None,
129129
**kwargs
130130
) -> Cursor:
131-
_id = kwargs.pop("id", None)
132-
if _id is not None:
131+
_id = kwargs.pop("id", notset)
132+
if _id != notset:
133133
kwargs["_id"] = _id
134134

135135
collection_name = model.get_db_collection()

fastapi_contrib/db/models.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import copy
2-
31
from datetime import datetime
42
from typing import List, Optional
53

@@ -10,6 +8,13 @@
108
from fastapi_contrib.db.utils import get_db_client, get_next_id
119

1210

11+
class NotSet(object):
12+
...
13+
14+
15+
notset = NotSet()
16+
17+
1318
class MongoDBModel(BaseModel):
1419
"""
1520
Base Model to use for any information saving in MongoDB.

0 commit comments

Comments
 (0)