I'm using SQLAlchemy with Flask for trying building a module where the user enter some data. This input module is divided in three pages. (Shop, Record, Vector) Each page has a submit button and when pressed it redirect the user to the next step.
In each step the user enter data that will populate a different table. (step1=shop_data, step2=record_data, step3=vector_data)
class Record(db.Model):
def __init__(self, ...):
# ...
__tablename__ = 'record_data'
id = db.Column(db.Integer, primary_key=True)
# ...
abcs = relationship('ABC', back_populates='a')
class Shop(db.Model):
def __init__(self, ...):
#...
__tablename__ = 'shop_data'
id = db.Column(db.Integer, primary_key=True)
# ...
abcs = relationship('ABC', back_populates='b')
class Vector(db.Model):
def __init__(self, ...):
#...
__tablename__ = 'vector_data'
id = db.Column(db.Integer, primary_key=True)
#...
abcs = relationship('ABC', back_populates='c')
Each tables should have a relationship with the others, so I can display or delete all together by using the "id". For this reason (in my mind) I'm trying to build a relationship db of type Many-To-Many:
class ABC(db.Model):
record_id = db.Column(db.Integer, ForeignKey('record_data.id'), primary_key=True)
shop_id = db.Column(db.Integer, ForeignKey('shop_data.id'), primary_key=True)
vector_id = db.Column(db.Integer, ForeignKey('vector_data.id'), primary_key=True)
a = db.relationship('Record', back_populates='abcs')
b = db.relationship('Shop', back_populates='abcs')
c = db.relationship('Vector', back_populates='abcs')
def __repr__(self):
return f'{self.a} {self.b} {self.c}'
And this are the app.route for each step:
@app.route('/new_record', methods=['GET', 'POST'])
def new_record():
form1 = AddShopForm()
if form1.validate_on_submit():
# ...
shop_data = Shop(a,b,c,d)
flash("The data of SHOP has been submitted.")
return redirect(url_for('new_record_part2'))
else:
# show validaton errors
return render_template('new_record.html', form=form1)
@app.route('/new_record_part2', methods=['GET', 'POST'])
def new_record_part2():
form2 = AddOperationForm()
if form2.validate_on_submit():
# ...
record_data = Record(e,f,g,h,i)
flash("The data of VECTOR has been submitted.")
return redirect(url_for('new_record_part3'))
else:
# show validaton errors
return render_template('new_record_part2.html', form=form2)
@app.route('/new_record_part3', methods=['GET', 'POST'])
def new_record_part3():
form3 = SelectVectorForm()
if form3.validate_on_submit():
# ...
vector_data = Vector(l,m,n,o)
flash("The data of RECORD has been submitted.")
return redirect(url_for('index'))
else:
# show validaton errors
return render_template('new_record_part3.html', form=form3)
At the last step I need something like this:
newRecord = ABC(a=record_data, b=shop_data, c=vector_data)
db.session.add(newRecord)
db.session.commit()
My question is: How i could pass the data (step1=shop_data, step2=record_data, step3=vector_data) between the functions and commit to the database only when reached and completed the third and last step?