Code

Opened 6 years ago

Closed 6 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: kmtracey
Component: Forms Version: master
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: UI/UX:

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

Download all attachments as: .zip

Change History (13)

Changed 6 years ago by romke <rombar@…>

patch for this one

comment:1 Changed 6 years ago by romke <rombar@…>

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by mtredinnick

  • 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 Changed 6 years ago by mtredinnick

  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by romke <rombar@…>

patch using get(name) as suggested

Changed 6 years ago by romke <rombar@…>

tested as suggested

comment:4 Changed 6 years ago by romke <rombar@…>

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

Changed 6 years ago by romke <rombar@…>

test for fix

comment:5 Changed 6 years ago by romke <rombar@…>

  • Needs tests unset
  • Patch needs improvement unset

better patch added, tests added

comment:6 Changed 6 years ago by julianb

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

comment:7 Changed 6 years ago by sime

  • Cc sime added

comment:8 Changed 6 years ago by kmtracey

  • Owner changed from nobody to kmtracey
  • Status changed from new to 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.

comment:9 Changed 6 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from assigned to closed

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.