Tyring to set up modelview with Flask-Admin causes ImportError

前端 未结 1 788
傲寒
傲寒 2020-11-28 16:31

I am trying to add a User model view to Flask-Admin. However, I get ImportError: cannot import name db. Why is this happening and how do I fix it?

相关标签:
1条回答
  • 2020-11-28 17:06

    Your code has the line from app.models import User in __init__.py. The problem is that app.models has from . import db. This is a circular import: __init__ tries to import User, which tries to import db, which isn't defined until after __init__ tries to import User. To solve this, move your local app imports below the definitions of all the global extension stuff.

    Currently, your code looks something like:

    from flask_sqlalchemy import SQLAlchemy
    from app.models import User
    
    db = SQLAlchemy()
    

    You need to change it to:

    from flask_sqlalchemy import SQLAlchemy
    
    db = SQLAlchemy()
    
    from app.models import User
    
    0 讨论(0)
提交回复
热议问题