gpt4 book ai didi

mysql - 无法导入名称 'Datetime' python flask sqlalchemy

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:22 24 4
gpt4 key购买 nike

我正在尝试运行 Flask - SQLAlchemy 应用程序。我想要做的是在我的表 usuario 中的每个插入中默认添加创建日期时间。到目前为止,我有 2 个文件:

在这里,我创建了 Usuario 类来创建具有一些信息的用户,并将日期时间添加为默认值。

database_setup.py

import datetime
import os
import sys
# in the next line, i get the error: ImportError: cannot import name 'Datetime'
from sqlalchemy import Column, ForeignKey, Integer, String, Datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base=declarative_base()


class Usuario(Base):
__tablename__='usuario'

id=Column(Integer, primary_key=True)
usuario_id=Column(String(255), nullable=False)
password=Column(String(200), nullable=False)
email=Column(String(255), nullable=False)
nombres=Column(String(300), nullable=False)
apellidos=Column(String(300), nullable=False)
# Here, I want to set a default value for each user
fecha_creacion=Column(Datetime, nullable=False, default=datetime.utcnow)

# String connection to mysql database
engine=create_engine('mysql://user:password@localhost/bdname')


Base.metadata.create_all(engine)

问题就在这里,当我尝试从控制台运行这个文件时:

python3 project.py

我得到了下一行:

File "project.py", line 4, in from database_setup import Base, Usuario
File "/home/facturacion/compartido/Cambio/database_setup.py", line 4, in
from sqlalchemy import Column, ForeignKey, Integer, String, Datetime
importError: cannot import name 'Datetime'

项目.py

from flask import Flask, flash, render_template, request, redirect, jsonify, url_for, session
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Usuario
import os

app = Flask(__name__)

engine = create_engine('mysql://root:facturacion@localhost/cambio')
Base.metadata.bind = engine

DBSession = sessionmaker(bind = engine)
dbsession = DBSession()

@app.route('/')
def index():
if 'username' in session:
return 'Logged in as %s' % session['username']
return 'Aun no se encuentra logueado'

@app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'POST':
usuario_frompost = request.form['username']
pass_frompost = request.form['password']
user = None
if '@' in usuario_frompost:
user = dbsession.query(Usuario).filter_by(email = usuario_frompost, password = pass_frompost)
else:
user = dbsession.query(Usuario).filter_by(usuario_id = usuario_frompost, password = pass_frompost)

if user:
flash('Login exitoso')
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=password name=password>
<p><input type=submit value=Login>
</form>
'''

@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))

if __name__ == '__main__':
app.debug = True
app.secret_key = os.urandom(24)
app.run(host = '0.0.0.0', port = 5000)

最佳答案

很简单,只是一个错字:

from sqlalchemy import DateTime

日期时间 不是日期时间

关于mysql - 无法导入名称 'Datetime' python flask sqlalchemy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com