在模型文件中使用管理配置

时间:2013-08-11 07:39:36

标签: python python-2.7 peewee

以下代码正常运行。

run.py

import datetime
from flask import Flask
from peewee import TextField, DateTimeField
from flask_peewee.db import Database
from flask_peewee.auth import Auth
from flask_peewee.admin import Admin, ModelAdmin

# configure database
DATABASE = {
    'name': 'surveyApp.db',
    'engine': 'peewee.SqliteDatabase',
}
DEBUG = True
CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'

app = Flask(__name__)
app.config.from_object(__name__)

db = Database(app)

# from model.user_infos import Note, NoteAdmin

class Note(db.Model):
    message = TextField()
    created = DateTimeField(default=datetime.datetime.now)

class NoteAdmin(ModelAdmin):
    columns = ('message', 'created',)

# create an Auth object for use with our flask app and database wrapper
auth = Auth(app, db)
admin = Admin(app, auth)
admin.register(Note, NoteAdmin)
admin.setup()

if __name__ == '__main__':
    app.run()  

我正在尝试将模型分开并在管理员中注册它。但它产生了错误。所有型号都在内部'模型'文件夹中。

生成错误的代码:

run.py(已修改)

import datetime
from flask import Flask
from peewee import TextField, DateTimeField
from flask_peewee.db import Database
from flask_peewee.auth import Auth
from flask_peewee.admin import Admin, ModelAdmin

# configure database
DATABASE = {
    'name': 'surveyApp.db',
    'engine': 'peewee.SqliteDatabase',
}
DEBUG = True
CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'

app = Flask(__name__)
app.config.from_object(__name__)

db = Database(app)

auth = Auth(app, db)
admin = Admin(app, auth)

from model.user_infos import Note, NoteAdmin # GENERATES IMPORT ERROR
admin.register(Note, NoteAdmin)
admin.setup()

if __name__ == '__main__':
    app.run()  

模型/ user_infos.py

from run import db, admin

import datetime
from peewee import TextField, DateTimeField
from flask_peewee.admin import ModelAdmin

class Note(db.Model):
    message = TextField()
    created = DateTimeField(default=datetime.datetime.now)

class NoteAdmin(ModelAdmin):
    columns = ('message', 'created',)

模型文件夹也包含__init__.py

1 个答案:

答案 0 :(得分:1)