Hello,

while working on a mechanism for optimistic offline locking for django 
modelforms we discovered the following issue:


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
in addition 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

The question is: Is this considered as a bug or is it just some feature not 
used the desired way

any comment on this are welcome

cs



A simplified way to reproduce this: 

-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


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)
else:
instance = None

if request.method == 'POST':
form = MyModelForm(request.POST, instance=instance)

if form.is_valid():
instance = form.save()
return HttpResponse('Success')
else:
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) 




-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/Umu0mUELRvsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to