未知
django-object-utils的Python项目详细描述
django对象实用程序
这个小库为更新django模型对象提供了帮助,而无需争用条件。
安装
pip install django-object-utils
用法
fromdjango_object_utilsimportreload_object,update_object,lock_objectuser=User.objects.get(pk=1)# Reload object in-place with the latest version from the databasereload_object(user)# Run SQL UPDATE for certain fields only and update the Python objectupdate_object(user,username='john',email='john@gmail.com')# Run SQL UPDATE and then reload the object from the databaseupdate_object(user,balance=F('balance')+payment,reload=True)ifrequest.method=='POST':withtransaction.atomic():# Enter a critical section and ensure that the object is the latest version.# Use the corresponding database row as the synchronization monitor.lock_object(user)form=UserForm(data=request.POST)form.save()