To use our poems database in our application we need to define a model. A model basically tells our Python application what the corresponding table in our database looks like, including what the field names are and what type of data is stored in them. If our database had multiple tables, we would create multiple models.
We define our model with peewee like this:
class Poem(Model):
body = CharField()
created_at = DateField()
class Meta:
database = db
See the full code for app.py up to this point:
# app.py
from flask import Flask, render_template, redirect, url_for, jsonify
import poembot
from peewee import *
app = Flask(__name__)
db = SqliteDatabase('poems.db')
@app.route('/')
def hello():
return "Hello world!"
@app.route('/poem')
def poem():
poem = poembot.generate_poem()
return render_template('poem.html', poem=poem)
class Poem(Model):
body = CharField()
created_at = DateField()
class Meta:
database = db
if __name__ == '__main__':
app.run(debug=True)