diff --git a/AUTHORS b/AUTHORS index 1bd0863fff..bf7ee55533 100644 --- a/AUTHORS +++ b/AUTHORS @@ -385,6 +385,7 @@ answer newbie questions, and generally made Django that much better: Vinay Sajip Kadesarin Sanjek Massimo Scamarcia + Paulo Scardine David Schein Bernd Schlapsi schwank@gmail.com diff --git a/django/forms/models.py b/django/forms/models.py index 3f7621e5e7..ca98658748 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -702,7 +702,7 @@ def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.f class BaseInlineFormSet(BaseModelFormSet): """A formset for child objects related to a parent.""" 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 if instance is None: self.instance = self.fk.rel.to() @@ -715,7 +715,9 @@ class BaseInlineFormSet(BaseModelFormSet): backlink_value = self.instance else: 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, queryset=qs) diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index d6f541bfd5..6b70826ab5 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/tests/modeltests/model_formsets/models.py @@ -644,6 +644,53 @@ True >>> formset.save() [, ] +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() +

+

+

+

+

+ +>>> 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() +

+

+

+>>> 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 ###################################################