I am trying to figure out the best way to pass data between Flask requests when using redirect. Here is some of my code first,
models
class Customer(db.Model):
__tablename__ = 'customers'
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(80))
last_name = db.Column(db.String(80))
email = db.Column(db.String(120), unique=True)
phone_number = db.Column(db.String(10), unique=True)
address = db.Column(db.Text)
last_order = db.Column(db.Text)
send_email = db.Column(db.Boolean)
def __init__(self, first_name, last_name, email, phone_number, address,
last_order, send_email):
self.first_name = first_name
self.last_name = last_name
self.email = email
self.phone_number = phone_number
self.address = address
self.last_order = last_order
self.send_email = send_email
def __repr__(self):
return '<Customer {} {}>'.format(self.first_name, self.last_name)
views
blueprint = Blueprint('customer', __name__, url_prefix='/customer')
@blueprint.route('/', methods=('GET', 'POST'))
@login_required
def home():
"""Logged-in user homepage"""
error = None
form = CustomerSearchForm()
if form.validate_on_submit():
phone_number = form.phone_number.data
customer = Customer.query.filter_by(phone_number=phone_number).first()
if customer:
return redirect(url_for('customer.customer', customer_id=customer.id))
else:
error = 'Customer with phone number {} not found'.format(phone_number)
form.phone_number.data = ''
return render_template('customer/index.html', form=form, error=error)
@blueprint.route('/<int:customer_id>', methods=('GET', 'POST'))
@login_required
def customer(customer_id):
"""Get customer by id"""
customer = Customer.query.get(customer_id)
return render_template('customer/customer.html', customer=customer, form=form)
So, write now, when I add a customer and I pass the id to the view customer with redirect(url_for('customer.customer')), I have to search for the customer again in the database and then render my template. I wanted help in determining if this is a correct/good approach? I know I cannot use g because doing redirect means a new request and hence g will not persist.
I can use session but then I found out that I have to do something like
session['customer'] = customer
where customer is an instance of Customer model, I will have a write a custom JsonEncoder where I will define how to encode the customer instance (Fixing the class to enable object storing in Flask session).
So, is it better to retrieve customer from database again using provided customer id or store customer object in session (using the custom JsonEncoder) or is there another better approach?