Code

Opened 2 years ago

Closed 2 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

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

Download all attachments as: .zip

Change History (9)

Changed 2 years ago by agriffis

comment:1 Changed 2 years ago by agriffis

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

comment:2 Changed 2 years ago by agriffis

  • Type changed from Uncategorized to Bug

comment:3 Changed 2 years ago by claudep

  • Needs tests set

comment:4 Changed 2 years ago by charettes

  • Cc charette.s@… added

Changed 2 years ago by claudep

With test

comment:5 Changed 2 years ago by claudep

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

comment:6 Changed 2 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 2 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.

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.