Code

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#11735 closed (fixed)

Wrong documentation for changing Model formsets queryset

Reported by: claudep 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 timo 5 years ago.
switch the order of super/self.queryset lines

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by timo

switch the order of super/self.queryset lines

comment:1 Changed 5 years ago by timo

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:2 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:3 Changed 4 years ago by russellm

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

Backport of r13553 from trunk.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.