这里我具有以下目录结构,
src/models/UserModel.py
src/resources/UserResources.py
和UserModel.py
包含
from datetime import datetime
from run import db
from passlib.hash import pbkdf2_sha256 as sha256
from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, ForeignKey
from flask import jsonify
class UserModel(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(120), unique = True, nullable = False)
password = db.Column(db.String(120), nullable = False)
user_role = db.Column(db.String(10), nullable = False)
access_token = db.Column(db.String(120), unique = True, nullable = True)
refresh_token = db.Column(db.String(120), unique = True, nullable = True)
def save_to_db(self):
db.session.add(self)
db.session.commit()
和UserResources.py
文件将UserModel
导入from models import UserModel
即使models目录具有__init__.py
文件,它也会引发以下错误ImportError: cannot import name 'UserModel' from 'models'
我在这里做错了什么?
答案 0 :(得分:1)
尝试使用:
from src.models.UserModel import UserModel