Opened 9 years ago
Closed 9 years ago
#26655 closed Bug (wontfix)
Provide a hook to handle database exceptions in Django Admin
Reported by: | Nick Retallack | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Steps to reproduce:
- Create a database model with a unique constraint
- Create a record in the django admin
- Create another record with the same value in that field.
The result is an unhandled database exception. It would be better if the exception was handled and reported as an error on the form. Here is a simple patch that demonstrates how you could do this.
https://github.com/nickretallack/django/commit/9353970aa0f169e44175a155a4a9e69daf8bda18
This method could be overridden to attach the error to the form in a more specific way, for example by introspecting the error's diagnostic information from the database to determine what the problem was and which form field it should be attached to.
Change History (4)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
Oh, you're right. I started a fresh project and it reports the error correctly there. Must be our fault.
I traced through the code to see where it does the unique check. Couldn't this fall victim to a race condition though? You could still end up seeing the database exception if two people try to create the same thing at the same time.
comment:3 by , 9 years ago
There's a small chance a race condition might occur between the validation and save attempts but that's generalized to all usages of model_form.is_valid() and model_form.save()
.
I suppose we could wrap the save calls in a transaction.atomic()
context and make sure to rerun validation and set form_validated=False
but I'm not sure this is worth it as the probability and severity of the failure is very low.
comment:4 by , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Discussion perhaps to be continued on a related django-developers thread.
If you're getting an unhandled database exception (I suppose an integrity error) that's the actual issue that should be solved here.
Could you provide a detailed step by step report about how to reproduce it?