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: romke <rombar@…> 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

  1. create model with custom primary_key:
class Test(models.Model):
  testing = models.CharField(max_length=20, primary_key=True)

  1. create form:
class TestForm(forms.ModelForm):
  class Meta:
    model = Test
    fields = ('testing')
  1. POST EMPTY testing field
KeyError @ forms/models.py in validate_unique, line 235 will pop up

Attachments (4)

key-error-on-empty-pk-field-in-forms-fix.patch (660 bytes ) - added by romke <rombar@…> 16 years ago.
patch for this one
key-error-on-empty-pk-field-in-forms-fix-using-get.patch (628 bytes ) - added by romke <rombar@…> 16 years ago.
patch using get(name) as suggested
runtests.log (13.5 KB ) - added by romke <rombar@…> 16 years ago.
tested as suggested
key-error-on-empty-pk-field-in-forms-fix-test.patch (861 bytes ) - added by romke <rombar@…> 16 years ago.
test for fix

Download all attachments as: .zip

Change History (13)

by romke <rombar@…>, 16 years ago

patch for this one

comment:1 by romke <rombar@…>, 16 years ago

Has patch: set

comment:2 by Malcolm Tredinnick, 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 Malcolm Tredinnick, 16 years ago

Triage Stage: UnreviewedAccepted

by romke <rombar@…>, 16 years ago

patch using get(name) as suggested

by romke <rombar@…>, 16 years ago

Attachment: runtests.log added

tested as suggested

comment:4 by romke <rombar@…>, 16 years ago

Sorry, Did not understood part about creating test for it.
I'll attach test for that in few hours.

by romke <rombar@…>, 16 years ago

test for fix

comment:5 by romke <rombar@…>, 16 years ago

Needs tests: unset
Patch needs improvement: unset

better patch added, tests added

comment:6 by Julian Bez, 16 years ago

My form didn't work at all. I can confirm that this patch fixes it. Thanks!

comment:7 by sime, 16 years ago

Cc: sime added

comment:8 by Karen Tracey, 16 years ago

Owner: changed from nobody to Karen Tracey
Status: newassigned

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.

comment:9 by Karen Tracey, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [9342]) [1.0.X] Fixed #9218 -- Simplified the fix from #9039 and added tests to ensure this case doesn't break again (and that the simplification didn't break anything).

[9341] from trunk. Also updated svnmerge metadata.

Note: See TracTickets for help on using tickets.
Back to Top