diff --git a/flask_admin/model/base.py b/flask_admin/model/base.py index 21d0a57dc..9970b56f0 100755 --- a/flask_admin/model/base.py +++ b/flask_admin/model/base.py @@ -870,7 +870,7 @@ def _refresh_forms_cache(self): # Initialize frozen fields if not hasattr(self, "_saved_frozen_fields"): - self._saved_frozen_fields = None + self._frozen_id = None def _refresh_filters_cache(self): self._filters = self.get_filters() @@ -2119,15 +2119,21 @@ def create_view(self): if not self.can_create: return redirect(return_url) - form = self.create_form() + # handle frozen fields, coming from an edit form + existing_model = None + if self._frozen_id: + existing_model = self.get_one(self._frozen_id) + self._frozen_id = None + + form = self.create_form(obj=existing_model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) - # handle frozen fields, coming from an edit form - if self._saved_frozen_fields: - for (field, data) in self._saved_frozen_fields.items(): - getattr(form, field).data = data - self._saved_frozen_fields = None + if existing_model is not None: + # we need to reset excluded frozen fields here. + for field in self.excluded_frozen_fields: + if field in form.data: + getattr(form, field).data = "" if self.validate_form(form): # in versions 1.1.0 and before, this returns a boolean @@ -2143,7 +2149,7 @@ def create_view(self): for field in self.excluded_frozen_fields: if field in form.data: getattr(form, field).data = "" - self._saved_frozen_fields = form.data + self._frozen_id = self.get_pk_value(model) return redirect(request.url) elif '_continue_editing' in request.form: # if we have a valid model, try to go to the edit view @@ -2199,12 +2205,7 @@ def edit_view(self): if '_add_another' in request.form: return redirect(self.get_url('.create_view', url=return_url)) elif '_freeze_fields' in request.form and self.allow_frozen_fields: - # we simply don't redirect here, which will cause the fields to - # stay as they are. All we need to do is reset the excluded fields. - for field in self.excluded_frozen_fields: - if field in form.data: - getattr(form, field).data = "" - self._saved_frozen_fields = form.data + self._frozen_id = self.get_pk_value(model) return redirect(self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(self.get_url('.edit_view', id=self.get_pk_value(model)))