nosql后端的流模型更新
django_nosql的Python项目详细描述
django nosql
Stream model changes to an upstream NoSQL database
支持的后端:
- 消防商店
- 模拟
todo后端
- 重新思考数据库
- Postgres(jsonfield)
- 休息
- redis
- ETCD
安装
pip install django-nosql
设置
添加到已安装的应用程序:
INSTALLED_APPS=[...,'django_nosql',...]
配置nosql后端:
在settings.py中:
# you can have multiple backends:NOSQL_BACKENDS=['firestore']# FireStore settingsFIRESTORE_CREDENTIALS_FILE='/path/to/credentials.json'
标记您的型号:
在models.py
Todo(models.model):# the nosql collection you'd like to usecollection='todos'# A Django Rest Framework serializer for serializing your instanceserializer_path='example_app.models.TodoSerializer'# inform django_nosql that you'd like to sync this modelreadonly_sync=True
添加信号:
fromdjango_nosql.signalsimport(sync_readonly_db,sync_remove_readonly_db)
测试:
此回购协议中包含一个示例应用程序。
若要查看正在运行的同步,请尝试。
python manage.py shell
或docker-compose run --rm web python manage.py shell
fromexample_app.modelsimportTodotodo=Todo.objects.create(text='Setup django nosql')# you should see this reflected in the 'todos' collection in Firebase# note: you need to manually refresh the db view when adding a new collection# you should see the rest of these updates in realtime# try update:todo.done=Truetodo.save()# you should see your change reflected in firestore# delete it:todo.delete()# it's gone from Firestore!
异步处理
如果您使用的是芹菜或django rq之类的东西,可以通过将基函数包装在@shared_task
中使信号异步。例如:
fromdjango.db.models.signalsimportpost_save,post_deletefromdjango.dispatchimportreceiverfromdjango.confimportsettingsfromdjango_nosql.signalsimport(sync_readonly_db,SYNC_TYPE)fromceleryimportshared_task# create a shared task that wraps `sync_readonly_db`@shared_taskdeffirebase_sync(instance,created):sync_readonly_db(instance,SYNC_TYPE.UPDATE,created)@shared_taskdeffirebase_sync_remove(instance):sync_readonly_db(instance,SYNC_TYPE.DELETE,False)# call that function in a delayed manner@receiver(post_save,dispatch_uid="django_nosql.sync")defsync_readonly_db(sender,instance,created,**kwargs):firebase_sync.delay(instance,created)@receiver(post_delete,dispatch_uid="django_nosql.sync.delete")defsync_remove_readonly_db(sender,instance,**kwargs):firebase_sync_remove.delay(instance)
贡献
部署到PIP
# bumpversion:
bumpversion {major|minor|patch}
# push to gitlab
git push origin master
# gitlab CI does the rest