DEV Community

tp1050
tp1050

Posted on

Creating forms from reflected orm objects FLASK/ALCHEMY BLEH

Philosophy:
We use computers to automate things, we use programming languages to automate the automation. When a framework makes you do stuff by hand either a) The framework is to put it academically "SHITTY" or it was not written with automation in mind.

Creating forms is pain, creating Database models is a pain.

We have a db objects that reflects all the tables in our DB:

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(flask_appinstance)
db.init_app(flask_appinstance)
db.Model.metadata.reflect(db.engine)

then you could reflect a table from your database simply by:

class Table_FROM_REAL_DB(db.Model):
__tablename__='Name_of_Tabble_In_Real_DB'

Nou you can easily create a form that accepts data from a webpage for this very table or loads data into an html table from this very table by simply:

`
from flask_wtf import FlaskForm
from wtforms_sqlalchemy.orm import model_form
instance_of_object_corrosponding_to_a_table=Name_of_Tabble_In_Real_DB()
Form_of_Tabble_In_Real_DB = model_form(model=Name_of_Tabble_In_Real_DB, base_class=FlaskForm, db_session=db.session)

populate it as per:
form.populate_obj(instance_of_object_corrosponding_to_a_table)

`
People who make things complicated have unresolved mental issues they do not want to deal with in real life and bring into sacred grounds of coding, they must stop touching the keyboard and find a therapist/partner/book.

Top comments (0)