简体   繁体   中英

PYTHON WTForms flask - saving data from drop down list to database

New to python and flask.

Trying to save data from drop down list to database using WTForms.

The aim is to get the data from table Choice(db.model) .

upload it to each field to choose from -> Choice(FlaskForm)

And store chosen data in Order(db.model)

I did managet to understand how to get three drop down lists. But I am missing the point how to save chosen data to Order table

   form = ChoiceForm(csrf_enabled=True)
    if form.validate_on_submit():
        ''' get instance of version from form data'''
        item = Order(
                    form.x1.data,
                    form.opts.data,
                    form.xx.data,
                    )

        db.session.add(item)
        db.session.commit()

This code above is the part I need tu figure out (how to commit the data to database order)

Below is the code

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms_sqlalchemy.fields import QuerySelectField

app = Flask(__name__)

app.secret_key = "Secret Key"
 
#SqlAlchemy Database Configuration With Mysql
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///leodat1.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

class Choice(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    extra = db.Column(db.String(50))

    def __repr__(self):
        return 'Choice {}'.format(self.name)

    def __init__(self, name, extra):
        self.name = name 
        self.extra = extra

def choice_query():
    return Choice.query
def choice_query_x():
    return Choice.query
def choice_query_x1():
    return Choice.query




class ChoiceForm(FlaskForm):
    opts = QuerySelectField(query_factory=choice_query, allow_blank=False, get_label='extra')
    xx = QuerySelectField(query_factory=choice_query_x, allow_blank=False, get_label='extra')
    x1 = QuerySelectField(query_factory=choice_query_x1, allow_blank=False, get_label='extra')


class Order(db.Model):
    order_id = db.Column(db.Integer, primary_key=True)
    item_01 = db.Column(db.String(50))
    item_02 = db.Column(db.String(50))
    choice_id = db.Column(db.Integer, db.ForeignKey('choice.id'))
    Choice = db.relationship('Choice', foreign_keys=[choice_id])


    def __init__(self, order_id, item_01, item_02):
        self.order_id = order_id 
        self.item_01 = item_01
        self.item_02 = item_02

        ''' I can not get it how can I save data from drop down list'''
@app.route('/', methods=['GET', 'POST'])
def index():
    form = ChoiceForm(csrf_enabled=True)
    if form.validate_on_submit():
        ''' get instance of version from form data'''
        item = Order(
                    form.x1.data,
                    form.opts.data,
                    form.xx.data,
                    )

        db.session.add(item)
        db.session.commit()


        return render_template('result.html',form=form)

    return render_template('index.html',form=form)


@app.route('/', methods=['GET', 'POST'])
def result():

    return render_template('result.html', form = form, xx = xx, x1 = x1)



if __name__ == '__main__':
    app.run(debug=True)

I think the problem is with this statement:

item = Order(
                    form.x1.data,
                    form.opts.data,
                    form.xx.data,
                    )

Try changing it to

item = Order( var1 = form.x1.data, var2 = form.opts.data, var3 = form.xx.data)

where var1, var2 and var3 are the column names for the Order table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM