2

刚接触python 世界,建议尝试cookiecutter-flask但遇到了一个问题:

我“手动”生成了迁移,而不是基于模型进行迁移。在我意识到模型可以用来生成迁移,就像 cookiecutter 附带的“库存”一样 - 我删除了我的手动迁移,但似乎无法让模型生成迁移文件。

在 app.py 中

from project import commands, public, user, category

def register_blueprints(app):
"""Register Flask blueprints."""
app.register_blueprint(public.views.blueprint)
app.register_blueprint(user.views.blueprint)
app.register_blueprint(category.views.blueprint) <- my model
return None

在视图中

blueprint = Blueprint('category', __name__, url_prefix='/categories', static_folder='../static')

我的路线似乎已被检测到

#flask urls
/categories/                                  category.categories          
/categories/static/<path:filename>            category.static              

但是当我跑步时

#flask db migrate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'users'
INFO  [alembic.autogenerate.compare] Detected added table 'roles'

它似乎无法检测类别模型,我对我在这里缺少什么感到迷茫?

4

1 回答 1

1

哦,所以问题最终变成了这样:

我的模型基于 cookiecutter 附带的用户模型。在视图中,我们从不导入显然应该导入的用户模型,而是该机制似乎依赖于 public/views.py 导入它

from project.user.models import User

而且因为在 register_blueprints() public 之前被加载,我们在处理用户视图时已经可以访问 User 模型。

所以 tl;dr 它需要在whatevermodel/views.py 中导入为

from project.category.models import Category

在我看来,这听起来有点太神奇了,并且依赖于在用户之前加载公共......

于 2017-04-20T09:03:07.340 回答