Skip to main content

Soft delete for MongoEngine

Project description

CircleCI Codacy Badge

MongoEngine Soft Delete

Make a document soft deletable.


Install mongoengine-softdelete through pip as usual:

pip install mongoengine-softdelete


Here is an example on how to use a soft deletable document:

from mongoengine_softdelete.document import SoftDeleteNoCacheDocument    

class IceCream(SoftDeleteNoCacheDocument):
    meta = {
        'collection': 'ice_cream',
        'soft_delete': {'deleted': True},
        'indexes': [ 'flavor' ],
        'strict': False

    flavor = fields.StringField(required=True)
    color = fields.StringField(required=True)
    price = fields.FloatField(default=0)
    created_at = fields.DateTimeField()

    # Declare the field used to check if the record is soft deleted
    # this field must also be reported in the `meta['soft_delete']` dict
    deleted = fields.BooleanField(default=False)

# Save a new document
ice = IceCream(flavor="Vanilla", color="White").save()
assert not ice.is_soft_deleted

# Mark the document as soft deleted
assert len(IceCream.objects()) == 0
assert ice.is_soft_deleted

# Soft undelete the document
assert len(IceCream.objects()) > 0
assert not ice.is_soft_deleted


The test suit requires that you run a local instance of MongoDB on the standard port and have pytest installed.
You can run tests with the pytest command or with make test.

Linting is done with mypy and pycodestyle with the make lint command.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongoengine-softdelete-0.0.10.tar.gz (3.7 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page