Tyring to set up modelview with Flask-Admin causes ImportError





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















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?



app/__init__.py:



from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import flask_admin as admin
from app.models import User

db = SQLAlchemy()
admin = admin.Admin(name="Admin Panel")

def create_app(config_name):
app = Flask(__name__)
db.init_app(app)
admin.init_app(app)
admin.add_view(ModelView(User, db.session))
return app


app/models.py:



from . import db, login_manager

class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)


When I add admin.add_view(ModelView(User, db.session)), I have to add from app.models import User as well. I get this error after adding these lines:



Traceback (most recent call last):
File "manage.py", line 3, in <module>
from app.models import User, Role
File "/home/qadim/PycharmProjects/esouq/app/__init__.py", line 1, in <module>
from app.models import User
File "/home/qadim/PycharmProjects/esouq/app/models.py", line 9, in <module>
from . import db, login_manager
ImportError: cannot import name db









share|improve this question































    2















    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?



    app/__init__.py:



    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    import flask_admin as admin
    from app.models import User

    db = SQLAlchemy()
    admin = admin.Admin(name="Admin Panel")

    def create_app(config_name):
    app = Flask(__name__)
    db.init_app(app)
    admin.init_app(app)
    admin.add_view(ModelView(User, db.session))
    return app


    app/models.py:



    from . import db, login_manager

    class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)


    When I add admin.add_view(ModelView(User, db.session)), I have to add from app.models import User as well. I get this error after adding these lines:



    Traceback (most recent call last):
    File "manage.py", line 3, in <module>
    from app.models import User, Role
    File "/home/qadim/PycharmProjects/esouq/app/__init__.py", line 1, in <module>
    from app.models import User
    File "/home/qadim/PycharmProjects/esouq/app/models.py", line 9, in <module>
    from . import db, login_manager
    ImportError: cannot import name db









    share|improve this question



























      2












      2








      2








      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?



      app/__init__.py:



      from flask import Flask
      from flask_sqlalchemy import SQLAlchemy
      import flask_admin as admin
      from app.models import User

      db = SQLAlchemy()
      admin = admin.Admin(name="Admin Panel")

      def create_app(config_name):
      app = Flask(__name__)
      db.init_app(app)
      admin.init_app(app)
      admin.add_view(ModelView(User, db.session))
      return app


      app/models.py:



      from . import db, login_manager

      class User(UserMixin, db.Model):
      __tablename__ = 'users'
      id = db.Column(db.Integer, primary_key=True)


      When I add admin.add_view(ModelView(User, db.session)), I have to add from app.models import User as well. I get this error after adding these lines:



      Traceback (most recent call last):
      File "manage.py", line 3, in <module>
      from app.models import User, Role
      File "/home/qadim/PycharmProjects/esouq/app/__init__.py", line 1, in <module>
      from app.models import User
      File "/home/qadim/PycharmProjects/esouq/app/models.py", line 9, in <module>
      from . import db, login_manager
      ImportError: cannot import name db









      share|improve this question
















      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?



      app/__init__.py:



      from flask import Flask
      from flask_sqlalchemy import SQLAlchemy
      import flask_admin as admin
      from app.models import User

      db = SQLAlchemy()
      admin = admin.Admin(name="Admin Panel")

      def create_app(config_name):
      app = Flask(__name__)
      db.init_app(app)
      admin.init_app(app)
      admin.add_view(ModelView(User, db.session))
      return app


      app/models.py:



      from . import db, login_manager

      class User(UserMixin, db.Model):
      __tablename__ = 'users'
      id = db.Column(db.Integer, primary_key=True)


      When I add admin.add_view(ModelView(User, db.session)), I have to add from app.models import User as well. I get this error after adding these lines:



      Traceback (most recent call last):
      File "manage.py", line 3, in <module>
      from app.models import User, Role
      File "/home/qadim/PycharmProjects/esouq/app/__init__.py", line 1, in <module>
      from app.models import User
      File "/home/qadim/PycharmProjects/esouq/app/models.py", line 9, in <module>
      from . import db, login_manager
      ImportError: cannot import name db






      python flask flask-sqlalchemy flask-admin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 28 '15 at 18:21









      davidism

      66.3k12184194




      66.3k12184194










      asked May 28 '15 at 17:56









      qasimalbaqaliqasimalbaqali

      1,0081232




      1,0081232
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30513814%2ftyring-to-set-up-modelview-with-flask-admin-causes-importerror%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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





            share|improve this answer






























              2














              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





              share|improve this answer




























                2












                2








                2







                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





                share|improve this answer















                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 28 '15 at 18:27

























                answered May 28 '15 at 18:03









                davidismdavidism

                66.3k12184194




                66.3k12184194
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30513814%2ftyring-to-set-up-modelview-with-flask-admin-causes-importerror%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues