Open YKdvd opened 11 years ago
This is indeed a problem. I don't yet know what would be the best way to implement this (don't really like patching the call method).
The recommended way of defining model forms is always using 'only' Meta argument instead of include and exclude. When you always explicitly tell with only what fields the model form should consist of this problem doesn't occur.
From what I can tell, when WTForms creates a form and the passed "formdata" is not empty, any fields not present in formdata will get initialized to empty or default values, even if a passed "obj=" parameter has them. Understandable, since things like HTML checkboxes don't get POSTed and have to be assumed off if not present.
But for a ModelForm, if you include a model's field but don't render it in the form you send out, on the return POST that field obviously won't be present and will be blanked out in the form. If you then use populate_obj() on the model instance from the db, you'll accidentally wipe out the existing value?
Obviously fields should be rendered (or hidden) in these forms, but it seems a bit fragile when making changes, like adding a model field to the form but forgetting to render it out in the template. Would it be feasible to patch into the base WTForms Field.call() method, and keep track of which fields have been _call_ed (and implicitly rendered)? ModelForm could then have an "unconsumed()" method which would return a list of fields not called, and debug code could check and throw a fit if a the rendering routine doesn't render everything out.