title: Flask-SQLalchemy update the current_user
body: <p>How would I update the current users firstname and lastnames in my Flask-SQLAlchemy database?</p>
  
  <p>I have tried to use the following code based off of the flask-sqlalchemy documentation - however, it only commits the update for the email field and does not update the record for either the firstname or lastname fields.</p>
  
  <pre><code># views.py
  @users.route("/account", methods=['GET', 'POST'])
  @login_required
  def account():
  
      form = UpdateUserForm()
  
      if form.validate_on_submit():
  
          current_user.firstname = form.firstname.data
          current_user.lastname = form.lastname.data
          current_user.email = form.email.data
  
          db.session.commit()
          return redirect(url_for('users.account'))
  
      elif request.method == 'GET':
          form.firstname.data = current_user.firstname
          form.lastname.data = current_user.lastname
          form.email.data = current_user.email
  
      return render_template('account.html', form=form)
  </code></pre>
  
  <pre><code>#forms.py
  class UpdateUserForm(FlaskForm):
  
      email = StringField('Email',validators=[InputRequired(message = 'Enter a valid email'),Email()])
      firstname = StringField('First Name',validators=[InputRequired(message = 'Enter your first name')])
      lastname = StringField('Last Name',validators=[InputRequired(message = 'Enter your last name')])
      submit = SubmitField('Update')
  
      def validate_email(self,email):
          if User.query.filter_by(email=email.data).first():
              raise ValidationError('This email has been registered already!')
  </code></pre>
  
  <pre><code># Summarized Template &amp; Form Submission
  
    &lt;div class="tab-content" id="myTabContent"&gt;
      &lt;div class="tab-pane fade show active" id="account" role="tabpanel" aria-labelledby="account-tab"&gt;
        &lt;div class="col-5"&gt;
  
        &lt;/div&gt;
        &lt;div class="col-sm-12 col-md-7"&gt;
          &lt;form method="post" action="" enctype="multipart/form-data"&gt;
            {{ form.hidden_tag() }}
  
            &lt;div class="form-group"&gt;
              &lt;label class="small mb-0"&gt;{{ form.firstname.label }}&lt;/label&gt;
              {{ form.firstname(class="form-control") }}
            &lt;/div&gt;
            &lt;div class="form-group"&gt;
              &lt;label class="small mb-0"&gt;{{ form.lastname.label }}&lt;/label&gt;
              {{ form.lastname(class="form-control") }}
            &lt;/div&gt;
            &lt;div class="form-group"&gt;
              &lt;label class="small mb-0"&gt;{{ form.email.label }}&lt;/label&gt;
              {{ form.email(class="form-control") }}
            &lt;/div&gt;
            {{ form.submit(class="btn btn-primary") }}
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <pre><code># Model.py
  class User(db.Model, UserMixin):
      __tablename__ = 'users'
      id = db.Column(db.Integer,primary_key=True)
      selected_theme = db.Column(db.String(64),nullable=False,default='default')
      email = db.Column(db.String(64),unique=True,index=True)
      fullname = db.Column(db.String(64))
      firstname = db.Column(db.String(64))
      lastname = db.Column(db.String(64))
      password_hash = db.Column(db.String(128))
  
      def __init__(self,email,fullname,password, firstname, lastname):
          self.email = email
          self.fullname = fullname
          self.firstname = firstname
          self.lastname = lastname
          self.password_hash = generate_password_hash(password)
  
      def check_password(self,password):
          return check_password_hash(self.password_hash,password)
  
      def __repr__(self):
          return f"Username {self.username}"
  </code></pre>