Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9494 -- Ensure the foreign key in an inline formset is…

… 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...
commit 6c069d2a71e19955b34a9c5aecc4d2075a5d430a 1 parent da15165
Brian Rosner authored November 01, 2008
7  django/forms/models.py
@@ -539,6 +539,13 @@ def inlineformset_factory(parent_model, model, form=ModelForm,
539 539
     # enforce a max_num=1 when the foreign key to the parent model is unique.
540 540
     if fk.unique:
541 541
         max_num = 1
  542
+    if fields is not None:
  543
+        fields = list(fields)
  544
+        fields.append(fk.name)
  545
+    else:
  546
+        # get all the fields for this model that will be generated.
  547
+        fields = fields_for_model(model, fields, exclude, formfield_callback).keys()
  548
+        fields.append(fk.name)
542 549
     kwargs = {
543 550
         'form': form,
544 551
         'formfield_callback': formfield_callback,
15  tests/modeltests/model_formsets/models.py
@@ -696,6 +696,21 @@ def __unicode__(self):
696 696
 >>> formset.errors
697 697
 [{'__all__': [u'Revision with this Repository and Revision already exists.']}]
698 698
 
  699
+# unique_together with inlineformset_factory with overridden form fields
  700
+# Also see #9494
  701
+
  702
+>>> FormSet = inlineformset_factory(Repository, Revision, fields=('revision',), extra=1)
  703
+>>> data = {
  704
+...     'revision_set-TOTAL_FORMS': '1',
  705
+...     'revision_set-INITIAL_FORMS': '0',
  706
+...     'revision_set-0-repository': repository.pk,
  707
+...     'revision_set-0-revision': '146239817507f148d448db38840db7c3cbf47c76',
  708
+...     'revision_set-0-DELETE': '',
  709
+... }
  710
+>>> formset = FormSet(data, instance=repository)
  711
+>>> formset.is_valid()
  712
+False
  713
+
699 714
 # Use of callable defaults (see bug #7975).
700 715
 
701 716
 >>> person = Person.objects.create(name='Ringo')

0 notes on commit 6c069d2

Please sign in to comment.
Something went wrong with that request. Please try again.