﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17478	Overridding model formset queryset in BaseModelFormSet	contact@…	nobody	"In https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#changing-the-queryset there are two methods described to override default queryset. Second method won't work in Django 1.3:
{{{
from django.forms.models import BaseModelFormSet

class BaseAuthorFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseAuthorFormSet, self).__init__(*args, **kwargs)
        self.queryset = Author.objects.filter(name__startswith='O')
}}}

Calling !BaseModelFormSet !__init!__ will call !BaseFormSet's !__init!__ and it will in turn call self._construct_forms(). self._construct_forms use queryset we try to override but it will be to late for this when the program reach our self.queryset = Author.objects.filter(name!__startswith='O') line.

!BaseFormSet's _construct_forms is only called once to populate forms attribute. We can defer it a bit using properties to allow queryset to be overridden. So instead for:
{{{
    def _construct_forms(self):
        # instantiate all the forms and put them in self.forms
        self.forms = []
        for i in xrange(self.total_form_count()):
            self.forms.append(self._construct_form(i))
}}}

we can do:

{{{
    def _construct_forms(self):
        if hasattr(self, ""_forms""):
            return self._forms
        # instantiate all the forms and put them in self.forms
        self._forms = []
        for i in xrange(self.total_form_count()):
            self._forms.append(self._construct_form(i))

        return self._forms

    forms = property(_construct_forms)
}}}
This change will also require _construct_forms call to be removed from !BaseFormSet's !__init!__.

Deferring forms creation should give us some time to override queryset attribute in !BaseAuthorFormSet's !__init!__."	Bug	closed	Forms	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
