1

I'm running into a very strange issue where one form is initializing with the data from another form entirely. Here is the first view:

class UpdateProfileView(FormMixin, DetailView):
    form_class = UpdateProfileForm
    model = Profile
    template_name = 'profile/update.html'

    def get_context_data(self, **kwargs):
        self.object = self.get_object()
        context = super(UpdateProfileView, self).get_context_data(**kwargs)
        ...
        self.initial['description'] = profile.about

        context['form'] = self.get_form()
        return context
    ...

This is the form that will return the correct data. As soon as it is loaded, however, the following form will return the initialized data from the previous one, even from different sessions, browsers, and locations:

class BountyUpdateForm(forms.ModelForm):

    class Meta:
        model = Bounty
        fields = ("description", "banner")


class UpdateBountyView(UpdateView):
    form_class = BountyUpdateForm
    model = Bounty
    template_name = 'bounty/update.html'
    ...

    def get_context_data(self, **kwargs):
        context = super(UpdateBountyView, self).get_context_data(**kwargs)
        description = context['form']['description']
        value = description.value()
        # Value equals what was initialized by the previous form.

I'm really curious why these two forms are interacting in this way. Both form fields are called 'description', but that doesn't explain why the initial data from one would be crossing over to the other. Restarting the server seems to temporarily get the second form to show the correct values, but as soon as the first one is loaded, the second follows suit.

Any help would be greatly appreciated!

Maaack
  • 115
  • 10
  • Considered I might be initializing the form fields incorrectly, but was reminded that this is the documented way to do it: https://docs.djangoproject.com/en/1.8/ref/forms/api/#dynamic-initial-values – Maaack Aug 26 '15 at 21:13
  • Do you mean, even after the POST method to the view, the `description` is not updated? – Edwin Lunando Aug 27 '15 at 04:51
  • No, sorry if that was unclear. I mean just loading the 2nd form, I get the initial data from the 1st form, even though they are separate pages dealing with different models. – Maaack Aug 27 '15 at 06:35

1 Answers1

0

After some more searching, I was able to determine that my second view was having self.initial set to the same values as the first form by the time dispatch was being run. I couldn't determine why, but found these related questions:

Same problem, but no accepted answer: Django(trunk) and class based generic views: one form's initial data appearing in another one's

Different problem, but good answer: Setting initial formfield value from context data in Django class based view

My workaround was overriding get_initial() on my first form, instead of setting self.initial['description'] directly.

class UpdateProfileView(FormMixin, DetailView):
    form_class = UpdateProfileForm
    model = Profile
    template_name = 'profile/update.html'

    def get_initial(self):
        return {
            'description': self.object.about
        }

    def get_context_data(self, **kwargs):
        ...
        # Removed the following line #
        # self.initial['description'] = profile.about
        ...
        context['form'] = self.get_form()
        return context

Hope this helps anyone else who runs into this same problem. I wish I knew more about Django class-based views to be able to understand why this happens to begin with. However, I was unable to determine where self.initial was being set, beyond the empty dict in FormMixin().

Community
  • 1
  • 1
Maaack
  • 115
  • 10