> Django Reference suggests that we use Transaction Middleware in the > web applications.
Mm, the documentation doesn't suggest you use it, it merely details how to use it if you in fact need it. Whether you need it or not is a design decision you must make. > What is the usual practice followed here? Do we use Transaction > Middleware most of the time? You need to think about transactions and your application. That is, does your application actually require transactions? Do you have multiple database operations that really MUST be performed as a unit, or rolled back as a unit? In most web applications, that's not likely to be the case. Commercial websites, money exchanges, these sorts of operations normally require transactions, but does a To-Do list application, a blog, or a photo gallery? So you need to think about whether your application truly requires transactions. Most of my apps do not, though I do have one use case in one app where I am using transactions to ensure logical consistency. > > The reason why I am asking is due to the following reason: > In my code, I at many places do the following > Obj = Model(parm1, parm2) > Obj.save() > # Now store the id for future use > l.append(Obj.id) No, don't do the above. Instead, do: l.append(Obj) > Now, I think this sort of code should be very usual in Web > Applications. But I cannot understand how this will work, since as per > my understanding, the _id_ will not be obtained until the DB actually > assigns one. Since transaction middleware allows the transaction to be > commited only at the end, this should fail right? As per above, don't store the id for future use. You've already got a reference to the model object, Obj. Append that to your list instead, if you need to refer to it. You don't have to worry about the id, and you don't have to worry about transactions (whether or not you decide to use them). If you keep the reference to the Obj throughout your transaction, you need never refer to the id explicitly, just use the object reference. Then later, when you commit your transaction, your object *in its final form* is saved to the database, and an ID is assigned. Hope that helps, ---Peter --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

