Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10263 -- Added a queryset argument to BaseInlineFormSet, norma…

…lizing with BaseModelFormSet, and allowing for custom ordering/subsets in inlines. Thanks to Paulo Scardine for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11874 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 680268a6833d5b18c08f2e1d65840f8c12d13249 1 parent 7aaacb5
Russell Keith-Magee authored December 16, 2009
1  AUTHORS
@@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better:
385 385
     Vinay Sajip <vinay_sajip@yahoo.co.uk>
386 386
     Kadesarin Sanjek
387 387
     Massimo Scamarcia <massimo.scamarcia@gmail.com>
  388
+    Paulo Scardine <paulo@scardine.com.br>
388 389
     David Schein
389 390
     Bernd Schlapsi
390 391
     schwank@gmail.com
6  django/forms/models.py
@@ -702,7 +702,7 @@ def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.f
702 702
 class BaseInlineFormSet(BaseModelFormSet):
703 703
     """A formset for child objects related to a parent."""
704 704
     def __init__(self, data=None, files=None, instance=None,
705  
-                 save_as_new=False, prefix=None):
  705
+                 save_as_new=False, prefix=None, queryset=None):
706 706
         from django.db.models.fields.related import RelatedObject
707 707
         if instance is None:
708 708
             self.instance = self.fk.rel.to()
@@ -715,7 +715,9 @@ def __init__(self, data=None, files=None, instance=None,
715 715
             backlink_value = self.instance
716 716
         else:
717 717
             backlink_value = getattr(self.instance, self.fk.rel.field_name)
718  
-        qs = self.model._default_manager.filter(**{self.fk.name: backlink_value})
  718
+        if queryset is None:
  719
+            queryset = self.model._default_manager
  720
+        qs = queryset.filter(**{self.fk.name: backlink_value})
719 721
         super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
720 722
                                                 queryset=qs)
721 723
 
47  tests/modeltests/model_formsets/models.py
@@ -644,6 +644,53 @@ def __unicode__(self):
644 644
 >>> formset.save()
645 645
 [<Poem: Brooklyn Bridge>, <Poem: Brooklyn Bridge>]
646 646
 
  647
+We can provide a custom queryset to our InlineFormSet:
  648
+
  649
+>>> custom_qs = queryset=Book.objects.order_by('-title')
  650
+>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
  651
+>>> for form in formset.forms:
  652
+...     print form.as_p()
  653
+<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>
  654
+<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" value="Le Spleen de Paris" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id" /></p>
  655
+<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" value="5" id="id_book_set-2-id" /></p>
  656
+<p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p>
  657
+<p><label for="id_book_set-4-title">Title:</label> <input id="id_book_set-4-title" type="text" name="book_set-4-title" maxlength="100" /><input type="hidden" name="book_set-4-author" value="1" id="id_book_set-4-author" /><input type="hidden" name="book_set-4-id" id="id_book_set-4-id" /></p>
  658
+
  659
+>>> data = {
  660
+...     'book_set-TOTAL_FORMS': '5', # the number of forms rendered
  661
+...     'book_set-INITIAL_FORMS': '3', # the number of forms with initial data
  662
+...     'book_set-0-id': '1',
  663
+...     'book_set-0-title': 'Les Fleurs du Mal',
  664
+...     'book_set-1-id': '2',
  665
+...     'book_set-1-title': 'Le Spleen de Paris',
  666
+...     'book_set-2-id': '5',
  667
+...     'book_set-2-title': 'Flowers of Evil',
  668
+...     'book_set-3-title': 'Revue des deux mondes',
  669
+...     'book_set-4-title': '',
  670
+... }
  671
+>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
  672
+>>> formset.is_valid()
  673
+True
  674
+
  675
+>>> custom_qs = queryset=Book.objects.filter(title__startswith='F')
  676
+>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
  677
+>>> for form in formset.forms:
  678
+...     print form.as_p()
  679
+<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="5" id="id_book_set-0-id" /></p>
  680
+<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>
  681
+<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>
  682
+>>> data = {
  683
+...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
  684
+...     'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
  685
+...     'book_set-0-id': '5',
  686
+...     'book_set-0-title': 'Flowers of Evil',
  687
+...     'book_set-1-title': 'Revue des deux mondes',
  688
+...     'book_set-2-title': '',
  689
+... }
  690
+>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
  691
+>>> formset.is_valid()
  692
+True
  693
+
647 694
 
648 695
 # Test a custom primary key ###################################################
649 696
 

0 notes on commit 680268a

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