happywowwow
V2EX  ›  问与答

flask-SQLAlchemy 在 query 时候的问题

  •  
  •   happywowwow · Jan 21, 2015 · 4653 views
    This topic created in 4222 days ago, the information mentioned may be changed or developed.
    print (type(Archive.query.filter_by))
    print (type(Archive.query.filter_by(archive_name='aaa')))
    print (Archive.query.filter_by(archive_name='aaa'))
    print (type(Archive.query.filter_by(archive_name='aaa').first))
    print (type(Archive.query.filter_by(archive_name='aaa').first()))
    
    <type 'instancemethod'>
    <class 'flask_sqlalchemy.BaseQuery'>
    SELECT flask_archive.id AS flask_archive_id, flask_archive.archive_name AS flask_archive_archive_name 
    FROM flask_archive 
    WHERE flask_archive.archive_name = :archive_name_1
    <type 'instancemethod'>
    

    最后一个没有输出,也没有错误提示就退出了。pdb调试 出现
    "The program exited via sys.exit(). Exit status: -9"

    1. 直接在 python 互式里面,可以 query
    2. 写份脚本也可以
    3. 现在怀疑原因是项目里面循环 import 了什么包

    引用关系,
    主app 在myapp.py

    from flask import Flask
    app = Flask(__name__)
    app.config.from_object('config')
    from views import *
    if '__main__' == __name__:
    □□□□app.debug = True
    □□□□app.run()

    views.py

    from models import Archive, Tag, Comment, Post, db
    @app.route('/blabla')
    def index():
    □□□□print (type(Archive.query.filter_by))
    □□□□print (type(Archive.query.filter_by(archive_name='aaa')))
    □□□□print (Archive.query.filter_by(archive_name='aaa'))
    □□□□print (type(Archive.query.filter_by(archive_name='aaa').first))
    □□□□print (type(Archive.query.filter_by(archive_name='aaa').first()))
    □□□□a = Archive.query.filter_by(archive_name='aaa').first()
    □□□□return "Hello world"

    models.py

    from myapp import app
    from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
    db = SQLAlchemy(app)
    class Archive(db.Model):
    □□□□pass
    class Tag(db.Model):
    □□□□pass

    以上代码简化了的

    5 replies    2015-01-21 13:59:47 +08:00
    wuyu1998
        1
    wuyu1998  
       Jan 21, 2015
    将所以的from xxx import *改为import xxx
    wuyu1998
        2
    wuyu1998  
       Jan 21, 2015
    views.py

    from models import Archive, Tag, Comment, Post, db
    @app.route('/blabla')
    def index():
    □□□□q = Archive.query.filter_by(archive_name='aaa')
    □□□□print ('q: {0}'.format(q))
    □□□□obj = q.first()
    □□□□print ('obj: {0}'.format(obj))
    □□□□return "Hello world"
    wuyu1998
        3
    wuyu1998  
       Jan 21, 2015
    删除myapp.py中的
    from views import *
    wuyu1998
        4
    wuyu1998  
       Jan 21, 2015
    最关键的问题,models.py中没有建立字段,无法和数据库的表建立映射关系。
    class Archive(db.Model):
    □□□□pass
    happywowwow
        5
    happywowwow  
    OP
       Jan 21, 2015
    @wuyu1998
    1.删除myapp.py中的
    from views import * 那这不是什么没了?

    2. □□□□pass 我完整代码没贴上来而已。。。其实是有的。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1481 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 16:49 · PVG 00:49 · LAX 09:49 · JFK 12:49
    ♥ Do have faith in what you're doing.