Saturday, 15 March 2014

calling wtforms from sqlalchemy in flask -



calling wtforms from sqlalchemy in flask -

i'm building website has several survey forms.

class a(form): name = text('name') class b(form): name = text('name') class c(form): name = text('name') etc...

i have model holds info on these forms:

class forms(db.model): id = db.column(db.integer(), primary_key=true) name = db.column(db.string(64), unique=true) description = db.column(db.string(255)) wtform = ???db.column(db.string(255))??? # how store wtform [a, b, c, etc...]?

from menu user select particular form , directed to:

@app.route('/new/<int:id>', methods = ['get', 'post']) @login_required def new(id): info = forms.query.get(id) form = data.???wtform??? # how phone call particular wtform here?

i'm not sure how store , phone call wtform class sqlalchemy. best method deal sort of situation? should instead utilize multiple @app.route('/a'), @app.route('/b'), @app.route('/c') calls each form, increment redundancy, or store entire wtform class construction in database?

you define forms in code, there's no reason store them in database well. store reference form use, instantiate right form based on reference.

class formdef(base): __tablename__ = 'form_def' id = column(integer, primary_key=true) name = column(string, nullable=false, unique=true) description = column(string, nullable=false, default='') form_ref = column(string, nullable=false) # name of form class in illustration def get_form(instance): # given instance of formdef, form class name stored in form_ref my_package import forms # or wherever forms stored homecoming getattr(forms, instance.form_ref)

on side note, storing form class info in rows not seem right way solve whatever you're trying solve.

flask sqlalchemy wtforms

No comments:

Post a Comment