Git Product home page Git Product logo

Comments (8)

roman-right avatar roman-right commented on May 17, 2024 3

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.

rdfsx avatar rdfsx commented on May 17, 2024

I would like to know too

from beanie.

sushilkjaiswar avatar sushilkjaiswar commented on May 17, 2024

Thank you, Roman

I am referring to the document link you have posted but some how I missed this detail.

from beanie.

rdfsx avatar rdfsx commented on May 17, 2024

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/

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.

roman-right avatar roman-right commented on May 17, 2024

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.

rdfsx avatar rdfsx commented on May 17, 2024

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.

roman-right avatar roman-right commented on May 17, 2024

Hey @rdfsx ,
It was fixed in 1.8.3 - you can inherit documents with actions now.

from beanie.

rdfsx avatar rdfsx commented on May 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.