File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -1695,6 +1695,9 @@ async def save(
16951695 self .check ()
16961696 db = self ._get_db (pipeline )
16971697 document = jsonable_encoder (self .dict ())
1698+
1699+ # filter out values which are `None` because they are not valid in a HSET
1700+ document = {k : v for k , v in document .items () if v is not None }
16981701 # TODO: Wrap any Redis response errors in a custom exception?
16991702 await db .hset (self .key (), mapping = document )
17001703 return self
Original file line number Diff line number Diff line change @@ -878,6 +878,26 @@ async def test_xfix_queries(members, m):
878878
879879
880880@py_test_mark_asyncio
881+ async def test_none ():
882+ class ModelWithNoneDefault (HashModel ):
883+ test : Optional [str ] = Field (index = True , default = None )
884+
885+ class ModelWithStringDefault (HashModel ):
886+ test : Optional [str ] = Field (index = True , default = "None" )
887+
888+ await Migrator ().run ()
889+
890+ a = ModelWithNoneDefault ()
891+ await a .save ()
892+ res = await ModelWithNoneDefault .find (ModelWithNoneDefault .pk == a .pk ).first ()
893+ assert res .test is None
894+
895+ b = ModelWithStringDefault ()
896+ await b .save ()
897+ res = await ModelWithStringDefault .find (ModelWithStringDefault .pk == b .pk ).first ()
898+ assert res .test == "None"
899+
900+
881901async def test_update_validation ():
882902 class TestUpdate (HashModel ):
883903 name : str
Original file line number Diff line number Diff line change @@ -974,6 +974,26 @@ async def test_xfix_queries(m):
974974
975975
976976@py_test_mark_asyncio
977+ async def test_none ():
978+ class ModelWithNoneDefault (JsonModel ):
979+ test : Optional [str ] = Field (index = True , default = None )
980+
981+ class ModelWithStringDefault (JsonModel ):
982+ test : Optional [str ] = Field (index = True , default = "None" )
983+
984+ await Migrator ().run ()
985+
986+ a = ModelWithNoneDefault ()
987+ await a .save ()
988+ res = await ModelWithNoneDefault .find (ModelWithNoneDefault .pk == a .pk ).first ()
989+ assert res .test is None
990+
991+ b = ModelWithStringDefault ()
992+ await b .save ()
993+ res = await ModelWithStringDefault .find (ModelWithStringDefault .pk == b .pk ).first ()
994+ assert res .test == "None"
995+
996+
977997async def test_update_validation ():
978998
979999 class Embedded (EmbeddedJsonModel ):
You can’t perform that action at this time.
0 commit comments