49
loading...
This website collects cookies to deliver better user experience
from beanie import Document, Link
class Door(Document):
height: int = 2
width: int = 1
class House(Document):
name: str
door: Link[Door] # This is the link
from typing import List
from beanie import Document, Link
class Window(Document):
x: int = 10
y: int = 10
class House(Document):
name: str
door: Link[Door]
windows: List[Link[Window]] # This is the list of the links
insert(...)
replace(...)
save(...)
link_rule
parameterhouse.windows = [Window(x=100, y=100)]
house.name = "NEW NAME"
# The next call will insert a new window object
# and replace the house instance with updated data
await house.save(link_rule=WriteRules.WRITE)
# `insert` and `replace` methods will work the same way
link_rule
parameter WriteRules.DO_NOTHING
house.door.height = 3
house.name = "NEW NAME"
# The next call will just replace the house instance
# with new data, but the linked door object will not be synced
await house.replace(link_rule=WriteRules.DO_NOTHING)
# `insert` and `save` methods will work the same way
fetch_links
houses = await House.find(
House.name == "test",
fetch_links=True
).to_list()
Link
class. You can fetch them manually then.fetch_all_links
methodawait house.fetch_all_links()
Link
objects with them.await house.fetch_link(House.door)
door
field of the house
object.DeleteRules.DELETE_LINKS
value for the link_rule
parameterawait house.delete(link_rule=DeleteRules.DELETE_LINKS)
DO_NOTHING
ruleawait house.delete(link_rule=DeleteRules.DO_NOTHING)
insert
, replace
and etc.@before_event
and @after_event
decorators respectively.from beanie import Insert, Replace
class Sample(Document):
num: int
name: str
@before_event(Insert)
def capitalize_name(self):
self.name = self.name.capitalize()
@after_event(Replace)
def num_change(self):
self.num -= 1
from beanie import Insert, Replace
class Sample(Document):
num: int
name: str
@before_event([Insert, Replace])
def capitalize_name(self):
self.name = self.name.capitalize()
name
field value before each document insert and replacefrom beanie import Insert, Replace
class Sample(Document):
num: int
name: str
@after_event([Insert, Replace])
async def send_callback(self):
await client.send(self.id)
Settings
inner class explicitly.class Sample(Document):
num: int
name: str
class Settings:
use_cache = True
capacity
(the maximum number of the cached queries) and expiration time in the Settings
inner class.class Sample(Document):
num: int
name: str
class Settings:
use_cache = True
cache_expiration_time = datetime.timedelta(seconds=10)
cache_capacity = 5
# on the first call it will go to the database
samples = await Sample.find(num>10).to_list()
# on the second - it will use cache instead
samples = await Sample.find(num>10).to_list()
await asyncio.sleep(15)
# if the expiration time was reached
# it will go to the database again
samples = await Sample.find(num>10).to_list()
revision_id
together with the document and changes it on each document update. If the application with the old local copy of the document will try to change it, an exception will be raised. Only when the local copy will be synced with the database, the application will be allowed to change the data. It helps to avoid losses of data.Settings
inner class explicitly too.class Sample(Document):
num: int
name: str
class Settings:
use_revision = True
revision_id
value:s = await Sample.find_one(Sample.name="TestName")
s.num = 10
# If a concurrent process already changed the doc,
# the next operation will raise an error
await s.replace()
ignore_revision
await s.replace(ignore_revision=True)
Settings
inner class explicitly.class Sample(Document):
num: int
name: str
class Settings:
use_state_management = True
save_changes()
method should be used.s = await Sample.find_one(Sample.name == "Test")
s.num = 100
await s.save_changes()
save_changes()
method can be used only with already inserted documents.validate_assignment = True
. But unfortunately, this is a heavy operation and doesn't fit some use cases.validate_on_save
instead.Settings
inner class explicitly.class Sample(Document):
num: int
name: str
class Settings:
validate_on_save = True
sample = await Sample.find_one(Sample.name == "Test")
sample.num = "wrong value type"
# Next call will raise an error
await sample.replace()