Opened 16 years ago
Closed 16 years ago
#9218 closed (fixed)
KeyError in validate_unique when primary_key is in fields and it's empty in form
Reported by: | Owned by: | Karen Tracey | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | KeyError, validate_unique, custom primary_key | |
Cc: | rombar@…, sime | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
- create model with custom primary_key:
class Test(models.Model): testing = models.CharField(max_length=20, primary_key=True)
- create form:
class TestForm(forms.ModelForm): class Meta: model = Test fields = ('testing')
- POST EMPTY testing field
KeyError @ forms/models.py in validate_unique, line 235 will pop up
Attachments (4)
Change History (13)
by , 16 years ago
Attachment: | key-error-on-empty-pk-field-in-forms-fix.patch added |
---|
comment:1 by , 16 years ago
Has patch: | set |
---|
comment:2 by , 16 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
I think this could be better written as self.cleaned_data.get(name)
, since that will return None
when it's missing (and the following line won't execute if the name is missing from cleaned_data. So the code is a bit simpler.
Also, this should really have a test to make sure we don't reintroduce the problem. There's a regression tests file in the forms test directory.
comment:3 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 16 years ago
Attachment: | key-error-on-empty-pk-field-in-forms-fix-using-get.patch added |
---|
patch using get(name) as suggested
comment:4 by , 16 years ago
Sorry, Did not understood part about creating test for it.
I'll attach test for that in few hours.
by , 16 years ago
Attachment: | key-error-on-empty-pk-field-in-forms-fix-test.patch added |
---|
test for fix
comment:5 by , 16 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
better patch added, tests added
comment:6 by , 16 years ago
My form didn't work at all. I can confirm that this patch fixes it. Thanks!
comment:7 by , 16 years ago
Cc: | added |
---|
comment:8 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Minor, but the attached patch uses 2-space indents while the rest of the code in the file uses 4-space -- in the future please code patches in the style of the surrounding code.
This error was actually fixed by the fix for #9039 but that fix didn't use the simpler-code get approach suggested above. I'll fix that and add a test for this to ensure it doesn't get broken again in the future.
patch for this one