On Wed, Oct 5, 2011 at 8:15 PM, robinne wrote:
> @transaction.commit_on_success
> def Test(request):
> try:
> member = request.user.get_profile()
> Credit.objects.create(CreditDate=datetime.datetime.now(),
> Member=member, CreditAmount=25)
> Credit.objects.create(CreditDate=datetime.datetime.now(),
> Member=member, CreditAmount=25.0) #FAIL
>
> return render_to_response("Test.html")
> except(Exception),e:
> WriteToLog('error in test: ' + e.__str__())
> return HttpResponseServerError('error')
from the docs [1]:
"If the function returns successfully, then Django will commit all
work done within the function at that point. If the function raises an
exception, though, Django will roll back the transaction."
but you're catching any exception and returning an HttpResponse. from
the point of view of Python, that's a successful function return, so
the transaction is committed.
if you want both: transaction rollback, and exception catching (to
turn them into log+HttpError), i think you should write your own
decorator, maybe something like (untested!):
def rollback_and_error(f, *args, **kwargs):
try:
with transaction.commit_on_success():
return f(*args, **kwargs)
except(Exception),e:
WriteToLog('error in %s: %s' %(f.__name__, e.__str__()))
return HttpResponseServerError('error')
[1]:https://docs.djangoproject.com/en/1.3/topics/db/transactions/#django.db.transaction.commit_on_success
--
Javier
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.