Opened 3 years ago

Closed 3 years ago

#18988 closed Bug (duplicate)

initialization of hidden_initial_fields after a form was bound

Reported by: its@… Owned by: nobody
Component: Forms Version: 1.2
Severity: Normal Keywords: forms, show_hidden_initial, change_data
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


After a failed form validation this forms hidden_initial-fields (rendered via the field argument show_hidden_initial=True) will be rendered containing the submitted data of the corresponding regular fields not the data the hidden_initial_fields have submitted

This might cause confusion because: if the form was not saved the value was not saved in the database but is now considered as current db-state in the hidden_initial_field
If a succesive submit is triggered the form will now not identify a form.has_changed-event for the corresponding field

IMHO the hidden_initial_field should recieve its POST/GET data from the hidden_initial_field-data instead of the POST/GET data from the original field after a submit

--how to reproduce
-field ('label') has the argument show_hidden_initial=True
-open the view with an instance from the database
-change the value of the label field and save -> form will be declared invalid and form.label_initial will contain the new value despite that the form could not be saved
-in addition if save is clicked again the form is valid and form.changed_data will be empty

This problem occurred in version 1.2 but considering the implementation of BoundField this will also happen in version 1.4

class MyModel(models.Model)
    label = models.CharField()
class MyModelForm(forms.Form)
    def __init__(*args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        for f in self.fields.keys():
            self.fields[f].show_hidden_initial = True
    class Meta:
        model = MyModel
    def clean(self):
        cleaned_data = super(MyModelForm, self).clean()
        if self.changed_data:
            self._errors['label'] = ErrorList(['form has changed'])
        return cleaned_data

def myView(request, modelId=None):
    if modelId:
        instance = MyModel.objects.get(pk=modelId)
        instance = None
    if request.method == 'POST':
        form = MyModelForm(request.POST, instance=instance)
        if form.is_valid():
            instance =
            return HttpResponse('Success')
        form = MyModelForm(instance=instance)
    template = '<FORM method="POST" enctype="application/x-www-form-urlencoded"><table>{{form}}</table><input method="POST" name="submit" type="submit" value="Save"\></Form>'
    t = template.Template(output)
    c = template.Context({'form':form})
    return t.render(c)          

Change History (2)

comment:1 Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

After 5 weeks there is no reaction to our ticket.
This is very disappointing, we have a bug fix in mind, are willing to contribute,
but our momentum is brought to a halt here.

We really would appreciate any reaction from the gurus here.


comment:2 Changed 3 years ago by lukeplant

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

I'm pretty sure this is a duplicate of #12440, which I think also describes why this probably isn't a valid bug. I'm going to close as duplicate for that reason.

One reason you haven't got a response is that show_hidden_initial is a completely undocumented internal, and so is has_changed, so it's extremely hard to assess if they have bugs. The default assumption is "no, they work as intended, but might not do what you wanted them to do".

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