Code

Ticket #10263: inlineformset_queryset_parameter.diff

File inlineformset_queryset_parameter.diff, 5.5 KB (added by paulos, 5 years ago)
Line 
1Index: django-trunk/django/forms/models.py
2===================================================================
3--- django-trunk/django/forms/models.py (revision 9962)
4+++ django-trunk/django/forms/models.py (working copy)
5@@ -462,7 +462,7 @@
6 class BaseInlineFormSet(BaseModelFormSet):
7     """A formset for child objects related to a parent."""
8     def __init__(self, data=None, files=None, instance=None,
9-                 save_as_new=False, prefix=None):
10+                 save_as_new=False, prefix=None, queryset=None):
11         from django.db.models.fields.related import RelatedObject
12         if instance is None:
13             self.instance = self.model()
14@@ -471,7 +471,9 @@
15         self.save_as_new = save_as_new
16         # is there a better way to get the object descriptor?
17         self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
18-        qs = self.model._default_manager.filter(**{self.fk.name: self.instance})
19+        if queryset is None:
20+            queryset = self.model._default_manager
21+        qs = queryset.filter(**{self.fk.name: self.instance})
22         super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name,
23                                                 queryset=qs)
24 
25Index: django-trunk/tests/modeltests/model_formsets/models.py
26===================================================================
27--- django-trunk/tests/modeltests/model_formsets/models.py      (revision 9962)
28+++ django-trunk/tests/modeltests/model_formsets/models.py      (working copy)
29@@ -552,7 +552,48 @@
30 >>> formset.save()
31 [<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
32 
33+We can provide a custom queryset to our InlineFormSet:
34 
35+>>> custom_qs = queryset=Book.objects.order_by('-title')
36+>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
37+>>> for form in formset.forms:
38+...     print form.as_p()
39+<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>
40+<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>
41+<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>
42+<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>
43+<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>
44+>>> data = {
45+...     'book_set-TOTAL_FORMS': '5', # the number of forms rendered
46+...     'book_set-INITIAL_FORMS': '3', # the number of forms with initial data
47+...     'book_set-0-title': 'Les Fleurs du Mal',
48+...     'book_set-1-title': 'Le Spleen de Paris',
49+...     'book_set-2-title': 'Flowers of Evil',
50+...     'book_set-3-title': 'Revue des deux mondes',
51+...     'book_set-4-title': '',
52+... }
53+>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
54+>>> formset.is_valid()
55+True
56+>>> custom_qs = queryset=Book.objects.filter(title__startswith='F')
57+>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
58+>>> for form in formset.forms:
59+...     print form.as_p()
60+<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>
61+<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>
62+<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>
63+>>> data = {
64+...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
65+...     'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
66+...     'book_set-0-title': 'Flowers of Evil',
67+...     'book_set-1-title': 'Revue des deux mondes',
68+...     'book_set-2-title': '',
69+... }
70+>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
71+>>> formset.is_valid()
72+True
73+
74+
75 # Test a custom primary key ###################################################
76 
77 We need to ensure that it is displayed