A few suggestions :
Circumvent the problem with smarter design: don't store the money on the object, make the user's money the sum of all their transactions (credit - debit).
You get lesser performance, but you also get history!
Maybe you could try (not sure about that):
MyModel.objects.filter(money__gte = value, pk = self.pk).update(F...)
and inspect the return value (number of updated rows!).
Now, you'd need to make sure django does that in a single statement.
If that doesn't work, I think update is the way to go anyway, but it might get a bit messy.
F... is an F object whose syntax I don't have off the top of my head.
Hi everyone,I'm looking for the proper django way to do an update of an attribute on my model instance, but only if the attribute current value is checked agains't a condition, in an atomic way, something like this:def use_money(self, value):begin_transaction()real_money = F('money')if real_money >= value:self.money = F('money') - valueself.save()end_transaction()I want to make sure that I avoid race condition so money never goes below 0.
Can you help me out?Thanks,--
Sebastien
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/hr1fBuAcX3kJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
I think I didn't make what I meant clear enough:
What do you think about the following:
. Insert record
. Calculate balance by summing all records before (including) the one you just inserted (and I think you will agree this is not an extremely complex query)
. If balance is positive, it's approved (and you'd probably want to change some status field to reflect that)
. If balance is negative, it's refused - and you can change status (or delete, though I wouldn't recommend that)
Nothing prevents us from differentiating inserting a record and approving the transaction, right?
Depending on whether you use a status field or not, and which transactions you take into account to know whether you will approve, you can get on the safe side.
Just assuming that a pending transaction (that is, a transaction that has been inserted but not approved yet) will be approved should prevent approving a withdrawal you should be refusing (but could lead you to refuse one you should be approving if your process is too long)
The day performance becomes an issue, you can look into alternate solutions, such as indeed storing the current balance somewhere.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/x8CZ3-F30PsJ.
I think you could always send the signals yourself.
Wrap that along in a model method, and I don't see any issue with using the manager!
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/x8CZ3-F30PsJ.