Opened 5 years ago

Closed 5 years ago

#17759 closed Bug (duplicate)

FormMixin instantiates form with dict stored on the class

Reported by: Aron Grififs Owned by: nobody
Component: Generic views Version: 1.3
Severity: Normal Keywords:
Cc: aron@…, charette.s@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following form:

class MyForm(ModelForm):
  def __init__(self, *args, **kwargs):
    kwargs['initial']['foo'] = 'bar'
    super(MyForm, self).__init__(*args, **kwargs)

When used with Django's class-based generic views, this will cause all following form instantiations to have a default initial dict with the content {'foo': 'bar'} because the dict is inadvertently updated on the class.

The problem is the following code in [source:django/trunk/django/views/generic/edit.py generic/edit.py]:

class FormMixin(object):
    initial = {}

    def get_initial(self):
        return self.initial

    def get_form_kwargs(self):
        kwargs = {'initial': self.get_initial()}
        ...

I think the right fix here is to shallow-copy the dict in get_initial, will attach patch.

Attachments (2)

django-17759.patch (421 bytes) - added by Aron Grififs 5 years ago.
17759-2.diff (2.1 KB) - added by Claude Paroz 5 years ago.
With test

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by Aron Grififs

Attachment: django-17759.patch added

comment:1 Changed 5 years ago by Aron Grififs

Cc: aron@… added
Has patch: set

comment:2 Changed 5 years ago by Aron Grififs

Type: UncategorizedBug

comment:3 Changed 5 years ago by Claude Paroz

Needs tests: set

comment:4 Changed 5 years ago by Simon Charette

Cc: charette.s@… added

Changed 5 years ago by Claude Paroz

Attachment: 17759-2.diff added

With test

comment:5 Changed 5 years ago by Claude Paroz

Needs tests: unset
Triage Stage: UnreviewedAccepted

comment:6 Changed 5 years ago by alex.vidal@…

Will this ticket be closed before the release of 1.4? We currently have a monkeypatch in place to deal with the problem, but I'd like to remove it when 1.4 is official.

comment:7 Changed 5 years ago by Claude Paroz

Resolution: duplicate
Status: newclosed

This is in fact a duplicate of #16138. Thanks aaugustin for the notice.

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