#35909: Mutation of FormMixin.initial can cause a FormView with a formset to 
crash
----------------------------------+--------------------------------------
     Reporter:  David Sanders     |                    Owner:  (none)
         Type:  Bug               |                   Status:  closed
    Component:  Generic views     |                  Version:  5.1
     Severity:  Normal            |               Resolution:  invalid
     Keywords:  FormView FormSet  |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Changes (by Sarah Boyce):

 * resolution:   => invalid
 * status:  new => closed

Comment:

 Hi David, long time no see!

 So it looks like we don't have tests covering setting the initial data but
 I would expect folks to either set the `initial` attribute or override
 `get_initial()`, I also feel like that's in the docs although there isn't
 an explicit example: https://docs.djangoproject.com/en/5.1/ref/class-
 based-views/mixins-editing/#formmixin

 So in short, I think what they have done is not how it should be used and
 so I wouldn't call this a bug and would close as invalid

 I would be open to adding more test coverage (and maybe doc updates to
 make the above clearer)
 Something like (with better naming)...
 {{{#!diff
 --- a/tests/generic_views/test_edit.py
 +++ b/tests/generic_views/test_edit.py
 @@ -14,13 +14,26 @@ from .models import Artist, Author
  class FormMixinTests(SimpleTestCase):
      request_factory = RequestFactory()

 -    def test_initial_data(self):
 +    def test_initial_data_independent(self):
          """Test instance independence of initial data dict (see
 #16138)"""
          initial_1 = FormMixin().get_initial()
          initial_1["foo"] = "bar"
          initial_2 = FormMixin().get_initial()
          self.assertNotEqual(initial_1, initial_2)

 +    def test_set_initial_data(self):
 +        class InitialAttribute(FormMixin):
 +            initial = {"foo": "bar"}
 +
 +        class GetInitial(FormMixin):
 +            def get_initial(self):
 +                initial = super().get_initial()
 +                initial["foo"] = "bar"
 +                return initial
 +
 +        self.assertEqual(InitialAttribute().get_initial(), {"foo":
 "bar"})
 +        self.assertEqual(GetInitial().get_initial(), {"foo": "bar"})
 +
      def test_get_prefix(self):
          """Test prefix can be set (see #18872)"""
          test_string = "test"
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35909#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019326464d44-f177bf7b-e352-48bd-9164-f75ac44d3f38-000000%40eu-central-1.amazonses.com.

Reply via email to