views:

73

answers:

2

Suppose there are two (or more) django administrators who have read a database record and then change and save it. There is no database problem, but some administrators are going to be surprised that the record they wrote was overwritten.

Is this issue ever addressed? One way would be to have an explicit "edit in progress" button which sets a flag in the record. If another administrator reads the same record and then clicks his "edit in progress" he will be warned that there is a previous edit in progress. Or a field could be added to the record which is incremented when a record is saved. If the field is different from when the record was read, the administrator is warned that the record has been changed by someone else since he read it.

Is there a native django way of handling this?

+2  A: 

The Django admin does not implement any write conflict protection out of the box. It would not be hard to add it yourself. Personally, I would take the "version number field" approach.

Carl Meyer
A: 

Generally this is where you want to read up on your database's transaction-isolation features, because that's why it has them.

If you'd really rather not do that, various patterns exist for doing this at the application layer, but there is no canonical way to do it -- some people set a sort of "last access" timestamp and refuse to allow editing within a certain period after that, others set version numbers, etc., etc.

James Bennett
Erm... what? Are you really suggesting to maintain a DB transaction across separate HTTP requests? Otherwise DB transactions provide no answer to "two admins load up the same admin page, one's save overwrites the other." The database happily maintains isolated transactions and does exactly what you told it to, which is to save the state saved by the person who saves last. App-level solutions are the only available solutions here.
Carl Meyer