Tuesday, 20 August 2013

Django transaction.commit() needed after Model.objects.filter() when object was saved from another process

Django transaction.commit() needed after Model.objects.filter() when
object was saved from another process

I have two processes running the Django codebase, for various reasons, one
process will update an object as follows:
myObj.aField = "updated"
myObj.save()
And later on the other process attempts to read that object as follows:
def getObj(xxx):
objs = TheModel.objects.filter(xyz=xxx)
for obj in objs:
print obj.aField
When reading the value from the second process, I won't see the updated
value, instead I see the old value. The second time around I run the
function, I do see the change.
I've noticed that if from the second process (the one reading), I change
the function as follows, I get the updated value:
@transaction.commit_manually def getObj(xxx): objs =
TheModel.objects.filter(xyz=xxx) transaction.commit() for obj in objs:
print obj.aField
After adding the decorator @transaction.commit_manually and the
transaction.commit() line right below the filter() call, I do get the
updated value from the field (which was saved from the other process.)
Is there any reason why this is needed? And what is the impact/potential
issues of using transaction.commit() on a function that actually does not
update models at all? I'm not sure why it works, and if it makes sense, so
hopefully someone else ran into this issue.
Thanks,

No comments:

Post a Comment