MongoEngine / MongoEngine/mongoengine

Automatically check if a document is being created?

Open
#2,134 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

The context here is simple, I want to automatically maintain "created" and "last_updated" fields on the model level.

Since the .save() method can be ambiguous (could be either create/update) I couldn't figure out how to do this, specially considering that the python document object might not be in sync with the database document.

Obviously I could implement this at the application's business logic level.

Example :

class Test(Document):

    created = DateTimeField()
    last_updated = DateTimeField()

    def clean(self):

        self.last_updated = datetime.now()
        if self.being_created:
            self.created = datetime.now()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the model-level clean() and .save() entry points mentioned in the issue, focusing on how creation is distinguished from updates when the Python object may be out of sync with MongoDB. Define how created and last_updated should be maintained automatically, then verify the behavior for both newly created and existing documents.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.