Python 如何在Peewee中初始化db?
我是新来的。我需要运行一些项目。我创建了virtualenv并安装了所有的要求:Python 如何在Peewee中初始化db?,python,flask,peewee,flask-peewee,Python,Flask,Peewee,Flask Peewee,我是新来的。我需要运行一些项目。我创建了virtualenv并安装了所有的要求: Flask==0.10.1 Flask-Login==0.3.1 Flask-Testing==0.4.2 Flask-WTF==0.12 Jinja2==2.8 MarkupSafe==0.23 WTForms==2.0.2 Werkzeug==0.10.4 argparse==1.2.1 flask-peewee==0.6.6 funcsigs==0.4 itsdangerous==0.24 mock==1.3
Flask==0.10.1
Flask-Login==0.3.1
Flask-Testing==0.4.2
Flask-WTF==0.12
Jinja2==2.8
MarkupSafe==0.23
WTForms==2.0.2
Werkzeug==0.10.4
argparse==1.2.1
flask-peewee==0.6.6
funcsigs==0.4
itsdangerous==0.24
mock==1.3.0
nose==1.3.7
pbr==1.8.0
peewee==2.6.4
psycopg2==2.6.1
requests==2.7.0
six==1.9.0
wsgiref==0.1.2
wtf-peewee==0.2.3
from peewee import Model, CharField, IntegerField, DateTimeField, DecimalField, TextField, datetime as peewee_datetime
from playhouse.pool import PooledPostgresqlExtDatabase
from .config import BaseConfig
db = PooledPostgresqlExtDatabase(**BaseConfig.DATABASE)
db.commit_select = True
db.autorollback = True
class BaseModel(Model):
class Meta:
database = db
def save(self, **kwds):
with db.transaction():
Model.save(self, **kwds)
class Payment(BaseModel):
class Meta:
db_table = "payments"
card_number = CharField()
amount = DecimalField()
....
def init_db():
try:
db.connect()
map(lambda l: db.drop_table(l, True), (Payment,))
print "tables dropped"
map(lambda l: db.create_table(l, True), (Payment,))
print "tables created"
except:
db.rollback()
raise
此外,我还有启动项目的runserver.py脚本:
from myproject import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True, port=5000, host='0.0.0.0')
现在我想从models.py初始化数据库:
Flask==0.10.1
Flask-Login==0.3.1
Flask-Testing==0.4.2
Flask-WTF==0.12
Jinja2==2.8
MarkupSafe==0.23
WTForms==2.0.2
Werkzeug==0.10.4
argparse==1.2.1
flask-peewee==0.6.6
funcsigs==0.4
itsdangerous==0.24
mock==1.3.0
nose==1.3.7
pbr==1.8.0
peewee==2.6.4
psycopg2==2.6.1
requests==2.7.0
six==1.9.0
wsgiref==0.1.2
wtf-peewee==0.2.3
from peewee import Model, CharField, IntegerField, DateTimeField, DecimalField, TextField, datetime as peewee_datetime
from playhouse.pool import PooledPostgresqlExtDatabase
from .config import BaseConfig
db = PooledPostgresqlExtDatabase(**BaseConfig.DATABASE)
db.commit_select = True
db.autorollback = True
class BaseModel(Model):
class Meta:
database = db
def save(self, **kwds):
with db.transaction():
Model.save(self, **kwds)
class Payment(BaseModel):
class Meta:
db_table = "payments"
card_number = CharField()
amount = DecimalField()
....
def init_db():
try:
db.connect()
map(lambda l: db.drop_table(l, True), (Payment,))
print "tables dropped"
map(lambda l: db.create_table(l, True), (Payment,))
print "tables created"
except:
db.rollback()
raise
如何创建表?Peewee是否有类似Django命令“python manage.py migrate”的功能?
如何在Django中的“python manage.py shell”这样的项目中运行python shell
db.create_tables([Table1, Table2, Table3])
文件:使用要求中未列出的Anubhav Agarwal、peewee_迁移。我需要在这个堆栈中执行。ök,我知道了。在shell内部,我从models.py导入init_db()并运行它。不需要迁移。