Contents

  1. Query
  2. Sitografia

Query

>>> from yourapp import User
>>> me = User('admin', 'admin@example.com')
>>> db.session.add(me)
>>> db.session.commit()
----
>>> db.session.delete(me)
>>> db.session.commit()

>>> peter = User.query.filter_by(username='peter').first()
>>> peter.id
1
>>> peter.email
u'peter@example.org'
>>> missing = User.query.filter_by(username='missing').first()
>>> missing is None
True
>>> User.query.filter(User.email.endswith('@example.com')).all()
[<User u'admin'>, <User u'guest'>]
>>> User.query.limit(1).all()
[<User u'admin'>]
>>> User.query.get(1)
<User u'admin'>

Specia keyword: get_or_404() and first_or_404()

Sitografia

MyWiki: Flask/SQLalchemy (last edited 2016-09-26 06:03:03 by risca)