1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
from pydantic import BaseModel
from odmantic import Model, SyncEngine
class Player(Model):
name: str
game: str
engine = SyncEngine()
player_tlo = engine.find_one(Player, Player.name == "TLO")
print(repr(player_tlo))
#> Player(id=ObjectId(...), name='TLO', game='Starcraft')
# Create the structure of the patch object with pydantic
class PatchPlayerSchema(BaseModel):
name: str
game: str
# Create the patch object containing the new values
patch_object = PatchPlayerSchema(name="TheLittleOne", game="Starcraft II")
# Apply the patch to the instance
player_tlo.model_update(patch_object)
print(repr(player_tlo))
#> Player(id=ObjectId(...), name='TheLittleOne', game='Starcraft II')
# Finally persist again the new instance
engine.save(player_tlo)
|