Ali khan
Ali khan

Reputation: 585

Database based forms in Flask

I am new to Flask and bit confused about the database modeling for it. Please have my apologies if by any mean this isn't a question eligible for post.

I need to create a multi choice model field in Flask and I need to be able to access it from backend admin panel to set values for it. It does shows options in docs with WTF forms to create multiple choice field. I am confused that how to create forms attached to Database. Can someone clear it up for me because I am a Django user and in Django Forms and ModelForms have different approach so trying to understand what would it be in Flask. How to render Database based forms in Flask? How would I create a multi choice field with database created for it. Please help.

Upvotes: 2

Views: 1875

Answers (1)

Mudassar Hashmi
Mudassar Hashmi

Reputation: 2899

What you are looking for is SQLAlchemy built-in ORM to build forms from models or integrated to database. There are other options to overcome limitations of Flask ORM when needed. Following is the example that would give you some clarity.

from flask import Flask, render_template, redirect, flash
from flask.wtf import Form
from flask.ext.sqlalchemy import SQLAlchemy

from wtf.ext.sqlalchemy.orm import model_form


app=Flask(__app__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/employees.sqlite'
app.config['SQLALCHEMY_ECHO'] = True

# Here you initiate the ext 
db=SQLAlchemy(app)

#Let's define a model 
class Employee(db.Model)
    __tablename__ = 'employee'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False
    birthday = db.Column(db.Date, nullable=False

    def __repr__(self):
        return 'employee %s' %self.name 

# Now go to your shell inside your env not in gloabal shell outside your env and run this command.
# From your .py file where you've created all above configration first make an import to db from shell 
from file.py import db 
#Then create a database with following command in shell 
db.create.all()

#Your auto generated database based form below

EmployeeForm() = model_form(Employee, base_class=Form, field_args{'name':{'class':'employee'}})

#Let's create a view with model_form or database based form in your case.
@app.route('/', methods=['GET', 'POST'])
def index()
    #request.POST does same in Django or any other Python based web framework like Bottle, Tornado etc
    form = EmployeeForm()
    try:
        if form_validate_on_submit():
            employee=Employee() #load the model values
            form.populate_obj(Employee) #populates the form with respective values 
            db.session.add(employee) #gathers the session based data to be added in DB
            db.session.commit() #Adds data to DB
            flash('New Employee added to database successfully.') #Display a message to end user at front end.
            retrun redirect('/') # redirects upon success to your homepage.
    except Exception e:
        # logs the errors
        db.session.rollback()
        flash('There was a problem registering new employee. Please contact the site administrator at [email protected]')

        employee_list = Employe.query.all() #equailent to django style "item.objects.all() to show list of all existing items.
        return render_template('index.html', form=form, employee_list=employee_list)

In last line above you did three things. You got your form variable or context variable like you do in Django as "form" so your end user can enter data. Then you have your model data that is saved in db as "employee_list=employee_list" that will show all the list to end users. "flash" is just like Django messaging framework.

Now for multiple choices its model has same as djagno choice arguement for key value like below: With my experience I would suggest you to install "peewee" a simple ORM for Python connected Databases.

choices = (('key', 'value')('key', 'value'))
employee_type = db.Model(db.String(90), choices=('key1', 'key2)

Hope this helps.

Upvotes: 2

Related Questions