Ticket #10263: inline_formset_queryset_argument.diff

File inline_formset_queryset_argument.diff, 5.6 KB (added by paulos, 6 years ago)

New patch as sugested by Alex

  • django-trunk/django/forms/models.py

     
    462462class BaseInlineFormSet(BaseModelFormSet):
    463463    """A formset for child objects related to a parent."""
    464464    def __init__(self, data=None, files=None, instance=None,
    465                  save_as_new=False, prefix=None):
     465                 save_as_new=False, prefix=None, queryset=None):
    466466        from django.db.models.fields.related import RelatedObject
    467467        if instance is None:
    468468            self.instance = self.model()
     
    471471        self.save_as_new = save_as_new
    472472        # is there a better way to get the object descriptor?
    473473        self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
    474         qs = self.model._default_manager.filter(**{self.fk.name: self.instance})
     474        if queryset:
     475            qs = queryset
     476        else:
     477            qs = self.model._default_manager.filter(**{self.fk.name: self.instance})
    475478        super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name,
    476479                                                queryset=qs)
    477480
  • django-trunk/tests/modeltests/model_formsets/models.py

     
    552552>>> formset.save()
    553553[<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
    554554
     555We can provide a custom queryset to our InlineFormSet:
    555556
     557>>> custom_qs = queryset=Book.objects.filter(author=author).order_by('-title')
     558>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
     559>>> for form in formset.forms:
     560...     print form.as_p()
     561<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>
     562<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>
     563<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>
     564<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>
     565<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>
     566>>> data = {
     567...     'book_set-TOTAL_FORMS': '5', # the number of forms rendered
     568...     'book_set-INITIAL_FORMS': '3', # the number of forms with initial data
     569...     'book_set-0-title': 'Les Fleurs du Mal',
     570...     'book_set-1-title': 'Le Spleen de Paris',
     571...     'book_set-2-title': 'Flowers of Evil',
     572...     'book_set-3-title': 'Revue des deux mondes',
     573...     'book_set-4-title': '',
     574... }
     575>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
     576>>> formset.is_valid()
     577True
     578>>> custom_qs = queryset=Book.objects.filter(author=author, title__startswith='F')
     579>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
     580>>> for form in formset.forms:
     581...     print form.as_p()
     582<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>
     583<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>
     584<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>
     585>>> data = {
     586...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
     587...     'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
     588...     'book_set-0-title': 'Flowers of Evil',
     589...     'book_set-1-title': 'Revue des deux mondes',
     590...     'book_set-2-title': '',
     591... }
     592>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs)
     593>>> formset.is_valid()
     594True
     595
     596
    556597# Test a custom primary key ###################################################
    557598
    558599We need to ensure that it is displayed
Back to Top