title: Flask-appbuilder model column default value set from g.user does not work
body: <p>In my application I would like all tables save and read data by Company Id.
  All tables have FK to Company Id. I've override Login and Registration views to Login by providing Company, User and Password. Company Id is shown in &quot;g.user.cia_id_fk&quot; after user logged-in. cia_id_fk has been added to &quot;ab_user&quot;
  <a href="https://i.stack.imgur.com/s5Nuo.png" rel="nofollow noreferrer">enter image description here</a>
  My approach is to &quot;default&quot; cia_id_fk to &quot;g.user.cia_id_fk&quot; in the Model.
  I am following &quot;extendsecurity2&quot; example (<a href="https://github.com/dpgaspar/Flask-AppBuilder/blob/master/examples/extendsecurity2/app/models.py" rel="nofollow noreferrer">https://github.com/dpgaspar/Flask-AppBuilder/blob/master/examples/extendsecurity2/app/models.py</a>)  where &quot;Contact&quot; model Created_by column is defaulted to &quot;g.user.id&quot;</p>
  <h1>model.py</h1>
  <pre><code>class Company(Model):
      __tablename__ = &quot;company&quot;
      id = Column(Integer, primary_key=True)  
      code = Column(String(15), unique=True, nullable=False)
      name = Column(String(60), unique=False, nullable=False)
  
  class AlpUser(User):
      __tablename__ = 'ab_user'   
      cia_id_fk = Column(Integer, ForeignKey(&quot;company.id&quot;), nullable=False)
  
  class Status(Model):
      __tablename__ = &quot;WorkItemStatus&quot;
      id = Column(Integer, primary_key=True)      
      status = Column(String(30), nullable=False) 
  
      @declared_attr
      def cia_id_fk(cls):
          return Column(
              Integer, ForeignKey(&quot;company.id&quot;), default=cls.get_cia_id, nullable=False
          )
  
      @declared_attr
      def cia_id(cls):
          return relationship(
              &quot;Company&quot;,
              primaryjoin=&quot;%s.cia_id_fk == Company.id&quot; % cls.__name__,
              enable_typechecks=False,
          )   
  
      @classmethod
      def get_cia_id(cls):
          try:
              return g.user.cia_id
          except Exception:
              return None 
  </code></pre>
  <p>When try to add a record in &quot;status&quot; I get this error:
  Note that field &quot;cia_id_fk&quot; is not shown in the form.</p>
  <pre><code>integrity error: (sqlite3.IntegrityError) NOT NULL constraint failed: status.cia_id_fk 
  [SQL: INSERT INTO &quot;status&quot; (status, cia_id_fk) VALUES (?, ?)]
  [parameters: ('pending', None)]
  </code></pre>
  <p>This indicate that &quot;default&quot; did not work. I'd tried may variations but not luck. Note that Example mention above works fine but design is different and I would like something simpler.</p>
  <p>Any other approach (like set in pre form) is welcome.</p>
  <p>Thanks in advance for any help</p>