Skip to content

Commit

Permalink
[1.0.X] Fixed #9494 -- Ensure the foreign key in an inline formset is…
Browse files Browse the repository at this point in the history
… always present on the forms. Thanks Fugazi for the report.

Backport of r9326 from trunk

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9327 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
brosner committed Nov 1, 2008
1 parent da15165 commit 6c069d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django/forms/models.py
Expand Up @@ -539,6 +539,13 @@ def inlineformset_factory(parent_model, model, form=ModelForm,
# enforce a max_num=1 when the foreign key to the parent model is unique. # enforce a max_num=1 when the foreign key to the parent model is unique.
if fk.unique: if fk.unique:
max_num = 1 max_num = 1
if fields is not None:
fields = list(fields)
fields.append(fk.name)
else:
# get all the fields for this model that will be generated.
fields = fields_for_model(model, fields, exclude, formfield_callback).keys()
fields.append(fk.name)
kwargs = { kwargs = {
'form': form, 'form': form,
'formfield_callback': formfield_callback, 'formfield_callback': formfield_callback,
Expand Down
15 changes: 15 additions & 0 deletions tests/modeltests/model_formsets/models.py
Expand Up @@ -696,6 +696,21 @@ def __unicode__(self):
>>> formset.errors >>> formset.errors
[{'__all__': [u'Revision with this Repository and Revision already exists.']}] [{'__all__': [u'Revision with this Repository and Revision already exists.']}]
# unique_together with inlineformset_factory with overridden form fields
# Also see #9494
>>> FormSet = inlineformset_factory(Repository, Revision, fields=('revision',), extra=1)
>>> data = {
... 'revision_set-TOTAL_FORMS': '1',
... 'revision_set-INITIAL_FORMS': '0',
... 'revision_set-0-repository': repository.pk,
... 'revision_set-0-revision': '146239817507f148d448db38840db7c3cbf47c76',
... 'revision_set-0-DELETE': '',
... }
>>> formset = FormSet(data, instance=repository)
>>> formset.is_valid()
False
# Use of callable defaults (see bug #7975). # Use of callable defaults (see bug #7975).
>>> person = Person.objects.create(name='Ringo') >>> person = Person.objects.create(name='Ringo')
Expand Down

0 comments on commit 6c069d2

Please sign in to comment.