Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

If you recall, the Product model looks like the following lines of code in the models.py file:

class Product(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    name = db.Column(db.String(255)) 
    price = db.Column(db.Float) 
    category_id = db.Column(db.Integer,        db.ForeignKey('category.id')) 
    category = db.relationship( 
        'Category', backref=db.backref('products', lazy='dynamic') 
    ) 

First, we will create a ProductForm class in models.py; this will subclass the FlaskForm class, which is provided by flask_wtf, to represent the fields required on a webform:

from flask_wtf import FlaskForm from wtforms import StringField, DecimalField, SelectField class ProductForm(FlaskForm): name = StringField('Name') price = DecimalField('Price') ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content