-1

I have a base flask app that I have put together to build future projects from. I have it set up with an application factory method "create_app".

https://github.com/thenetimp/flask_base_v2/blob/master/app/init.py#L1-L32

In the create_app method, I am initializing the application object. Then pass it to the previously initialized db object eventually calling db.create_all to create the database from my model(s).

In order for this to work I have to import any model I may have into the create_app function. This isn't problematic for a small database with a few tables, but if I have a database with a large number of tables it seems like there should be a better way. from app.models import * doesn't work inside functions, so I have to ask is there another way to manage this?

thenetimp
  • 9,487
  • 5
  • 29
  • 42
  • Possible duplicate: https://stackoverflow.com/questions/54487519/how-to-get-flask-sqlalchemy-to-work-with-the-application-factory-pattern – djnz May 15 '19 at 21:09
  • Similar problem but not entirely a duplicate, eitherway I just figured out what I needed to change and it is working now. – thenetimp May 16 '19 at 03:48

1 Answers1

0

in the code I was trying to do

def create_app():
     from app.models import *
     ...

when all I needed to do was

def create_app():
     import app.models
     ...

get so use to doing it one way I forgot about the other.

thenetimp
  • 9,487
  • 5
  • 29
  • 42