0

I have a fully working Flask-Admin app that makes use of Flask-Login. In a number of places in my ModelView code, I make use of current_user, and this has always worked. For example, I have

def on_model_change(self, form, model, is_created):
  model.last_modified_by = current_user.get_id()

I'm now trying to disable editing on a field based on access level, and after I declare some form_args, I added:

if current_user.access_level == 'USER':
  form_widget_args = {'salary': {'disabled': True}}

This throws the error AttributeError: 'NoneType' object has no attribute 'access_level'. Yet without this code, I go right into the on_model_change I show above, and this successfully sets the last_modified_by value.

What could be going on?

user9219182
  • 329
  • 1
  • 9

1 Answers1

0

OK, after a bit more digging, I came across this question, which revealed what was going on. The value of form_widget_args was getting cached on app initialization, and wasn't getting checked when things were actually running (i.e. when there was a current_user).

I changed my code to this:

@property
def form_widget_args(self):
    if not has_app_context() or (current_user.access_level == 'USER'):
        form_widget_args = {'salary': {'disabled': True}}
        return form_widget_args

And everything is good.

user9219182
  • 329
  • 1
  • 9