Idea above
updated = Entry.objects.filter(Q(id=e.id) && Q(version=e.version))\ .update(updated_field=new_value, version=e.version+1) if not updated: raise ConcurrentModificationException()
Looks great and should work fine even without serializable transactions.
The problem is how to increase the behavior of deafult.save (), since there is no need to do manual tuning to call the .update () method.
I reviewed the idea of Custom Manager.
My plan is to override the Manager _update method, which Model.save_base () calls to perform the update.
This is the current code in Django 1.3
def _update(self, values, **kwargs): return self.get_query_set()._update(values, **kwargs)
What IMHO needs to do is something like:
def _update(self, values, **kwargs):
A similar thing should happen upon removal. However, uninstalling is a bit more complicated as Django implements quite some voodoo in this area through django.db.models.deletion.Collector.
It is strange that a modren tool such as Django does not have a guide for Optimictic Concurency Control.
I will update this post when I solve the riddle. We hope that the solution will be in a good pythonic key, which will not include tons of coding, strange views, skipping the main parts of Django, etc.
Kiril Jul 31 '11 at 10:40 2011-07-31 22:40
source share