miguelgrinberg / microblog

The microblogging application developed in my Flask Mega-Tutorial series. This version maps to the 2024 Edition of the tutorial.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
MIT License
4.56k stars 1.65k forks source link

ImportError: cannot import name 'Post' #207

Closed tosaiprakash closed 4 years ago

tosaiprakash commented 4 years ago

Please solve my problem, im facing so many problems ,but im sloving it by seeing all sources but seriously i dont have so much time , so im requesting u to help me to complete your microblog project , thank you sir

so this is my code from models.py

from datetime import datetime
from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')

    def __repr__(self):
        return '<User {}>'.format(self.username)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __repr__(self):
        return '<Post {}>'.format(self.body)
>>> **from app import db**
>>> **from app.models import User, Post**
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Post'
jdagnin commented 4 years ago

If im not mistaken, this could be caused by circular import

check inside /app/ init .py that from app import models appears as the last line, not at the top

miguelgrinberg commented 4 years ago

@tosaiprakash I suggest you compare your files against mine to find the problem, it's really hard for me to figure out what's wrong with just the one file that you shared. You can use the link at the top of the article to download the files that correspond to the chapter you are reading, that should help you figure out the problem.

tosaiprakash commented 4 years ago

thank you miguelgrinberg , really happy for getting replay from u ,