Skip to content

Commit

Permalink
Fixed #10263 -- Added a queryset argument to BaseInlineFormSet, norma…
Browse files Browse the repository at this point in the history
…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
freakboy3742 committed Dec 16, 2009
1 parent 7aaacb5 commit 680268a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better:
Vinay Sajip <vinay_sajip@yahoo.co.uk> Vinay Sajip <vinay_sajip@yahoo.co.uk>
Kadesarin Sanjek Kadesarin Sanjek
Massimo Scamarcia <massimo.scamarcia@gmail.com> Massimo Scamarcia <massimo.scamarcia@gmail.com>
Paulo Scardine <paulo@scardine.com.br>
David Schein David Schein
Bernd Schlapsi Bernd Schlapsi
schwank@gmail.com schwank@gmail.com
Expand Down
6 changes: 4 additions & 2 deletions django/forms/models.py
Expand Up @@ -702,7 +702,7 @@ def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.f
class BaseInlineFormSet(BaseModelFormSet): class BaseInlineFormSet(BaseModelFormSet):
"""A formset for child objects related to a parent.""" """A formset for child objects related to a parent."""
def __init__(self, data=None, files=None, instance=None, def __init__(self, data=None, files=None, instance=None,
save_as_new=False, prefix=None): save_as_new=False, prefix=None, queryset=None):
from django.db.models.fields.related import RelatedObject from django.db.models.fields.related import RelatedObject
if instance is None: if instance is None:
self.instance = self.fk.rel.to() self.instance = self.fk.rel.to()
Expand All @@ -715,7 +715,9 @@ def __init__(self, data=None, files=None, instance=None,
backlink_value = self.instance backlink_value = self.instance
else: else:
backlink_value = getattr(self.instance, self.fk.rel.field_name) backlink_value = getattr(self.instance, self.fk.rel.field_name)
qs = self.model._default_manager.filter(**{self.fk.name: backlink_value}) if queryset is None:
queryset = self.model._default_manager
qs = queryset.filter(**{self.fk.name: backlink_value})
super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
queryset=qs) queryset=qs)


Expand Down
47 changes: 47 additions & 0 deletions tests/modeltests/model_formsets/models.py
Expand Up @@ -644,6 +644,53 @@ def __unicode__(self):
>>> formset.save() >>> formset.save()
[<Poem: Brooklyn Bridge>, <Poem: Brooklyn Bridge>] [<Poem: Brooklyn Bridge>, <Poem: Brooklyn Bridge>]
We can provide a custom queryset to our InlineFormSet:
>>> custom_qs = queryset=Book.objects.order_by('-title')
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
>>> for form in formset.forms:
... print form.as_p()
<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>
<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>
<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>
<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>
<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>
>>> data = {
... 'book_set-TOTAL_FORMS': '5', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '3', # the number of forms with initial data
... 'book_set-0-id': '1',
... 'book_set-0-title': 'Les Fleurs du Mal',
... 'book_set-1-id': '2',
... 'book_set-1-title': 'Le Spleen de Paris',
... 'book_set-2-id': '5',
... 'book_set-2-title': 'Flowers of Evil',
... 'book_set-3-title': 'Revue des deux mondes',
... 'book_set-4-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
>>> formset.is_valid()
True
>>> custom_qs = queryset=Book.objects.filter(title__startswith='F')
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
>>> for form in formset.forms:
... print form.as_p()
<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>
<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>
<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>
>>> data = {
... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered
... 'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
... 'book_set-0-id': '5',
... 'book_set-0-title': 'Flowers of Evil',
... 'book_set-1-title': 'Revue des deux mondes',
... 'book_set-2-title': '',
... }
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
>>> formset.is_valid()
True
# Test a custom primary key ################################################### # Test a custom primary key ###################################################
Expand Down

0 comments on commit 680268a

Please sign in to comment.