Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#11735 closed (fixed)

Wrong documentation for changing Model formsets queryset

Reported by: Claude Paroz Owned by: nobody
Component: Documentation Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In the documentation, we can read that a Model Formset queryset can be changed with the following code:

class BaseAuthorFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        self.queryset = Author.objects.filter(name__startswith='O')
        super(BaseAuthorFormSet, self).__init__(*args, **kwargs)

See http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset

This doesn't work. The problem is that in the __init__ function of BaseModelFormSet, the queryset is redefined from the queryset parameter which default to None.
See http://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L448

To make it work, the queryset should be added to the __init__ call as a keyword parameter. Or it might also be a bug in the __init__ function where it should test if self.queryset is not None before assigning to it. Either in docs, either in code, I think there is something to solve.

Attachments (1)

11735.diff (618 bytes) - added by Tim Graham 7 years ago.
switch the order of super/self.queryset lines

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by Tim Graham

Attachment: 11735.diff added

switch the order of super/self.queryset lines

comment:1 Changed 7 years ago by Tim Graham

Has patch: set
Triage Stage: UnreviewedReady for checkin

comment:2 Changed 6 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [13553]) Fixed #11735 -- Corrected an example of FormSet subclassing. Thanks to claudep for the report.

comment:3 Changed 6 years ago by Russell Keith-Magee

(In [13560]) [1.2.X] Fixed #11735 -- Corrected an example of FormSet subclassing. Thanks to claudep for the report.

Backport of r13553 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top