Snippet List
If you want to test for trivial error in your add and changelist admin views, use this snippet.
Save the snippet in admintests.py and put it anywhere in your pythonpath.
Put this code in your tests.py:
from django.test import TestCase
from admintest import adminviews_test
class TestAdminViews(TestCase):
def test_admin_views(self):
adminviews_test(self)
- admin
- test
- automatic test
Look here: https://bitbucket.org/depaolim/optlock
The snippet of Marco De Paoli is much better than this one! :-)
=========================================================
If two users save same record, the second one will overwrite first one.
Use this snippet to achieve an optimistic locking, so the second user will get an exception.
Save the snippet in optlock.py and put it anywhere in your pythonpath.
**Do not put _version_opt_lock in readonly_fields** or the snippet will fail! (if you need to hide it, use jquery).
The snippet need in request.POST the original value of _version_opt_lock and if you make it a readonly field Django doesn't POST its value. When this [bug](https://code.djangoproject.com/ticket/11277) will be fixed it should be possible to use a hiddeninput widget.
models.py example:
...
import optlock
...
class YourModel(optlock.Model):
...
admin.py example:
...
import optlock
...
class YourModelAdmin(optlock.ModelAdmin):
...
That's it :-)
- admin
- locking
- optimistic locking
Taifu has posted 2 snippets.