1
Eae personal! I come to ask a force, I have a problem to accomplish the construction of the object to create the relationship of the tables. I have a driver’s table and another passenger and another still called a race. In the race table I need to pick up a driver and a passenger, but giving error: Nameerror: name 'driver' is not defined
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
#IMPORTAR FRAMEWORK FLASK
from flask import Flask, render_template, request, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
#CONFIGURAÇÃO E CRIACAO DAS TABELAS DO BACO DE DADOS
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
db = SQLAlchemy(app)
class Motorista(db.Model):
__tablename__ = 'motorista'
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
nomeMotorista = db.Column(db.String)
dtNascMotorista = db.Column(db.String)
cpfMotorista = db.Column(db.Integer)
modCarro = db.Column(db.String)
status = db.Column(db.String)
sexoMotorista = db.Column(db.String)
corrida = db.relationship('Corrida', backref='motorista', lazy='dynamic')
class Passageiro(db.Model):
__tablename__ = 'passageiro'
id = db.Column(db.Integer, autoincrement = True, primary_key = True)
nomePassageiro = db.Column(db.String)
dtNascPassageiro = db.Column(db.String)
cpfPassageiro = db.Column(db.Integer)
sexoPassageiro = db.Column(db.String)
corrida = db.relationship('Corrida', backref='passageiro', lazy='dynamic')
class Corrida(db.Model):
__tablename__ = 'corrida'
id = db.Column(db.Integer, autoincrement = True, primary_key = True)
motorista_id = db.Column(db.Integer, db.ForeignKey('motorista.id'))
passageiro_id = db.Column(db.Integer, db.ForeignKey('passageiro.id'))
valorCorrida = db.Column(db.Integer)
db.create_all()
##REGISTRO DE CORRIDAS
@app.route('/registrarCorrida')
def registrarCorrida():
return render_template('registroCorrida.html')
@app.route('/registroCorrida', methods=['GET', 'POST'])
def registroCorrida():
if request.method == 'POST':
valorCorrida = request.form.get('valorCorrida')
motorista_id = request.form.get('motorista_id')
passageiro_id = request.form.get('passageiro_id')
if valorCorrida and motorista_id and passageiro_id:
corrida = Corrida(valorCorrida = valorCorrida, motorista_id = motorista(motorista_id = motorista_id), passageiro_id = passageiro(passageiro_id = passageiro_id))
db.session.add(corrida)
db.session.commit()
return redirect(url_for('index'))
###############
if __name__ == '__main__':
app.run(debug=True)
Thank you very much!