Opened 5 years ago

Closed 4 years ago

#17759 closed Bug (duplicate)

FormMixin instantiates form with dict stored on the class

Reported by: agriffis 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


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/ generic/]:

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 agriffis 5 years ago.
17759-2.diff (2.1 KB) - added by claudep 5 years ago.
With test

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by agriffis

comment:1 Changed 5 years ago by agriffis

  • Cc aron@… added
  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by agriffis

  • Type changed from Uncategorized to Bug

comment:3 Changed 5 years ago by claudep

  • Needs tests set

comment:4 Changed 5 years ago by charettes

  • Cc charette.s@… added

Changed 5 years ago by claudep

With test

comment:5 Changed 5 years ago by claudep

  • Needs tests unset
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 4 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 4 years ago by claudep

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

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

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