code_junkie

Django: How can I protect against concurrent modification of data base entries

Nov 12th, 2011
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. UPDATE ... WHERE version = 'version_from_user';
  2.  
  3. updated = Entry.objects.filter(Q(id=e.id) && Q(version=e.version))
  4. .update(updated_field=new_value, version=e.version+1)
  5. if not updated:
  6. raise ConcurrentModificationException()
  7.  
  8. def save(self):
  9. if(self.id):
  10. foo = Foo.objects.get(pk=self.id)
  11. if(foo.timestamp > self.timestamp):
  12. raise Exception, "trying to save outdated Foo"
  13. super(Foo, self).save()
Add Comment
Please, Sign In to add comment