Flask admin remember form value Flask admin remember form value flask flask

Flask admin remember form value


I have come across this situation before and i have solved it using 2 functions. its pretty easy and small.

@expose('/edit/', methods=('GET', 'POST'))def edit_view(self):    #write your logic to populate the value into html            self._template_args["arg_name"] = stored_value    # in your html find this value to populate it as you need

the above function will let you populate the values in html when user tries to edit any value. This can be populated using the value stored. And below is a function that helps you save the value from previous edit.

within this class MyModelView(sqla.ModelView): you need to add the below 2 functions.

def on_model_change(self, form, model, is_created):    stored_value = model.user # this is your user name stored    # get the value of the column from your model and save it 

This is a 2 step operation that's pretty small and does not need a lot of time. I have added just a skeleton/pseudo code for now.


on_form_prefill will not help you with this problem, as it only works for the edit form.

As the lib code says -> on_form_prefill => Perform additional actions to pre-fill the edit form

When a request hit, the below flow of code happens:

(Base class) View(create_view) ->create_form->_create_form_class->get_create_form->get_form-> scaffold_form

Hence If we want to change the default value of the create form being loaded we can update it in the methods handling the form as explained in the above flow.

Hence we can override create_form.

 def create_form(self):     form = super().create_form()     # set the user id we want in the form as default.     form.user_id.default = 'USER_2_ID'     return form

This was to get the default value for the form.

To set the value we override the on_model_change

def on_model_change(self, form, model, is_created):    # set the USER_ID from this model.user_id    pass

Now the way to share this data(USER_ID) from the setter and getter methods are following,

  1. We set this in cookies and get on the create request.
  2. Update the "save and add another" button link to add the user_id in the query strings.

This data has to be shared between two different requests hence storing it in application context, g won't work. The application context "will not be shared between requests"

 def create_form(self):     form = super().create_form()     user_id = request.args.get('user_id')     form.user_id.default = user_id     # or if you want only one option to be available      user_query = self.session.query(User).filter(User.id = user_id).one()     form.user.query = [user_query]     return form

As the above answer mention to use edit_view, create_view, that can also be used to add

An easy option but not a good one will be to query on creating that will give you the last entry(But this approach is not optimal one)

@expose('/new/', methods=('GET', 'POST'))def create_view(self):    model = self.get_model()    # query the model here to get the latest value of the user_id    self._template_args['user_id'] = user_id    return super(YourAdmin, self).details_view()