Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#16701 closed Bug (duplicate)

FormMixin.initial is shared between all child classes

Reported by: wilfred@… Owned by: nobody
Component: Generic views Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


FormMixin in django/views/ has an initial class variable. This is bad because it is shared between classes. If I do the following:

class MyUpdateView(BaseUpdateView, TemplateView):
    form_class = ArtworkForm
    template_name = 'admin/admin__artwork_create_or_update.html'

    def get_initial(self):
        initial = super(MyUpdateView, self).get_initial()

        initial['level'] = self.artwork.level
        initial['artist'] = self.artwork.artist

        return initial

this causes me problems because I also have a create view:

class MyCreateView(BaseCreateView, TemplateView):
    template_name = 'admin/admin__artwork_create_or_update.html'
    form_class = ArtworkForm

and this create view ends up with the initial values I only want for updating. It would be much better if this were an instance attribute. Patch attached, I hope it's OK.

Attachments (1)

formmixin.diff (737 bytes) - added by wilfred@… 4 years ago.
Adding an init method to FormMixin so .initial is an instance attribute

Download all attachments as: .zip

Change History (3)

Changed 4 years ago by wilfred@…

Adding an init method to FormMixin so .initial is an instance attribute

comment:1 Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This is a duplicate of #16138.

On a side note, the file you uploaded contains escape codes for colors, making it invalid as a patch.

comment:2 Changed 4 years ago by wilfred@…

Ah, I was looking for tickets mentioning FormMixin. Sorry for the ticketspam.

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