Opened 13 years ago

Closed 13 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 13 years ago.
17759-2.diff (2.1 KB ) - added by Claude Paroz 13 years ago.
With test

Download all attachments as: .zip

Change History (9)

by Aron Grififs, 13 years ago

Attachment: django-17759.patch added

comment:1 by Aron Grififs, 13 years ago

Cc: aron@… added
Has patch: set

comment:2 by Aron Grififs, 13 years ago

Type: UncategorizedBug

comment:3 by Claude Paroz, 13 years ago

Needs tests: set

comment:4 by Simon Charette, 13 years ago

Cc: charette.s@… added

by Claude Paroz, 13 years ago

Attachment: 17759-2.diff added

With test

comment:5 by Claude Paroz, 13 years ago

Needs tests: unset
Triage Stage: UnreviewedAccepted

comment:6 by alex.vidal@…, 13 years ago

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 by Claude Paroz, 13 years ago

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