Comments (8)
Hey, sorry for the late reply.
I think you can do the thing with the next code:
from datetime import datetime
from typing import Optional
from pydantic import Field
from beanie import Document, before_event, Replace, Insert
class Sample(Document):
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: Optional[datetime] = None
@before_event([Insert, Replace])
def set_updated_at(self):
self.updated_at = datetime.utcnow()
The updated_at
field will be updated only before inserts and replaces here (and before save()
, as it is a combination of replace
and insert
methods). But probably this is enough.
The doc about before_event
could be found here: https://roman-right.github.io/beanie/tutorial/event-based-actions/
from beanie.
I would like to know too
from beanie.
Thank you, Roman
I am referring to the document link you have posted but some how I missed this detail.
from beanie.
Hey, sorry for the late reply.
I think you can do the thing with the next code:
from datetime import datetime from typing import Optional from pydantic import Field from beanie import Document, before_event, Replace, Insert class Sample(Document): created_at: datetime = Field(default_factory=datetime.utcnow) updated_at: Optional[datetime] = None @before_event([Insert, Replace]) def set_updated_at(self): self.updated_at = datetime.utcnow()The
updated_at
field will be updated only before inserts and replaces here (and beforesave()
, as it is a combination ofreplace
andinsert
methods). But probably this is enough.The doc about
before_event
could be found here: https://roman-right.github.io/beanie/tutorial/event-based-actions/
Can I use the Sample class as a mixin? The code below does not work (updated_at in ChatModel does not update)
class TimeBaseModel(Document):
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: Optional[datetime] = None
@before_event([Insert, Replace, SaveChanges])
def set_updated_at(self):
print("updated")
self.updated_at = datetime.utcnow()
class ChatModel(TimeBaseModel):
id: int = Field(alias="_id")
type: str = Field(...)
class Collection:
name = "ChatModel"`
from beanie.
Hey @rdfsx ,
Unfortunately, nope. I will fix this, but for now, it works only directly with the classes. It should be fixed in a week or two
from beanie.
Hey @rdfsx , Unfortunately, nope. I will fix this, but for now, it works only directly with the classes. It should be fixed in a week or two
Okay, thank you for your work!
from beanie.
Hey @rdfsx ,
It was fixed in 1.8.3 - you can inherit documents with actions now.
from beanie.
Hey @rdfsx ,
It was fixed in 1.8.3 - you can inherit documents with actions now.
Wow. Thank you so much!
from beanie.
Related Issues (20)
- [BUG] Typing errors whenever `Link[SomeDoc]` is used (Pylance) HOT 7
- [BUG] From 1.24.0 the exception RevisionIdWasChanged is raised differently from 1.23.6 HOT 4
- [BUG] before_event, after_event can not trigger HOT 2
- find_one with update ignores sorting HOT 7
- [BUG] Argument of type `Callable` to `json_schema_extra` raises `AttributeError` HOT 1
- [BUG] Backlinks are not populated HOT 9
- [BUG] Fetching Links in view raises AttributeError: _database_major_version HOT 2
- [BUG] Double return in sources code of class beanie.odm.fields.Link HOT 4
- [BUG] Beanie projection and Pydantic Schema do not play well together HOT 6
- [BUG] before_event not working with validate_on_save = True HOT 2
- [BUG] ModuleNotFoundError when importing models in a migration module HOT 1
- [BUG]before_event bug HOT 3
- [BUG] Missing documentation for `beanie.odm.BulkWriter` HOT 1
- [BUG] (some) type annotations for session wrong HOT 1
- [BUG] type annotation for init_beanie() should use Sequence
- [BUG] before_event of type Update HOT 4
- [BUG] mypy does not raise error when trying to access a non-existing attribute from a beanie Document. HOT 1
- [BUG] Indexed unique will not work after Field regex.
- [BUG] Empty BackLink for `Optional[BackLink[ADoc]]` when document has no back-link HOT 1
- Concerns and Suggestions Regarding Beanie Library HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from beanie.