https://github.com/abassel/mongosafe
Provides safe reference fields for Mongoengine and Flask-admin dashboard without the need to migrate to MongoMallard.
https://github.com/abassel/mongosafe
flask flask-admin mongoengine
Last synced: 18 days ago
JSON representation
Provides safe reference fields for Mongoengine and Flask-admin dashboard without the need to migrate to MongoMallard.
- Host: GitHub
- URL: https://github.com/abassel/mongosafe
- Owner: abassel
- License: mit
- Created: 2018-01-19T21:30:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-04-21T17:02:01.000Z (almost 5 years ago)
- Last Synced: 2025-10-26T08:30:18.515Z (5 months ago)
- Topics: flask, flask-admin, mongoengine
- Language: Python
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MongoSafe
[](https://travis-ci.org/abassel/mongosafe)
[](https://coveralls.io/github/abassel/mongosafe?branch=master)
[](https://badge.fury.io/py/mongosafe)
[](https://pypi.python.org/pypi/mongosafe)
Provides safe reference fields for Mongoengine and Flask-admin dashboard without the need to migrate to MongoMallard!
It is heavily based(stolen) on MongoMallard.
[Mongoengine](https://github.com/MongoEngine/mongoengine) is an ORM-like layer on top of PyMongo.
[Flask-admin](https://github.com/flask-admin/flask-admin) is a simple and extensible administrative interface framework for Flask.
[MongoMallard](https://hack.close.io/posts/mongomallard) is a fast ORM based on MongoEngine
> Please note: This may not be the fastest way to manipulate data but it protects you from null references that will break Flask-admin.
## Install
```bash
pip install mongosafe
```
## Example
In the example bellow, mongosafe handles missing references automatically:
```python
import mongoengine as me
from mongosafe import SafeReferenceField, SafeReferenceListField
class A(me.Document):
protectedBs = SafeReferenceListField(me.ReferenceField('B'))
unprotectedBs = me.ListField(me.ReferenceField('B'))
class B(me.Document):
title = me.StringField()
b1 = B()
b2 = B()
b1.save()
b2.save()
a = A()
a.protectedBs = [b1, b2]
a.unprotectedBs = [b1, b2]
a.save()
b2.delete()
a = A.objects().first()
print (a.protectedBs) # []
print (a.unprotectedBs) # [, DBRef('b', ObjectId('5a62438cfc701444a2e2107f'))]
```
## References :notebook:
- [MongoMallard](https://hack.close.io/posts/mongomallard)
- [Mongoengine](https://github.com/MongoEngine/mongoengine)
- [Flask-admin](https://github.com/flask-admin/flask-admin)