Basically I’m trying to send the form data from a queryselectfield to the database. The problem I am having is I can’t seem to find a way to format the data so as not to show both the object name and the actual object value.
From a table called company, I retrieve the three values stored in the table; Company A, Company B, Company C.
As an example, from the table called Company, if I do a print command once inside the form.validate function the results always show up as [Company Company A]. So I’m not able to add that data to another table as it doesn’t show as just Company A from the data.
The queryselectfield displays the company names just fine, but when reading the data after form.validate it interprets it as [Company Company A]
So to break it down…
models.py
class Company(db.Model):
id = db.Column(db.Integer, primary_key=True)
company = db.Column(db.String(50), unique=True)
contact = db.Column(db.String(50))
def __str__(self):
return '[Company {}]'.format(self.company)
forms.py
def company_query():
return Company.query
class ChooseCompanyForm(FlaskForm):
opts = QuerySelectField('Select Company', query_factory=company_query, allow_blank=False, get_label='company')
submit = SubmitField('Submit')
routes.py
@app.route("/new_ticket", methods=['GET', 'POST'])
def new_ticket():
form = ChooseCompanyForm()
form.opts.query = Company.query.all()
if form.validate_on_submit():
#company = request.form.get('opts') # tried it this way too
company = form.opts.data
#db.session.add(company)
#db.session.commit()
print(company) # for debugging
#print(form.opts.data) # for debugging
flash('A new ticket has been added to the database.', 'success')
return redirect(url_for('index'))
#return '<h1>{}</h1>'.format(form.opts.data) #for seeing returned values
return render_template('new_ticket.html', title='Create New Ticket', form=form)
templates/new_ticket.html
{% extends "layouts.html" %}
{% block content %}
<form action="" method="POST">
{{ form.csrf_token }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Ticket Information</legend>
<div class="form-group">
{{ form.opts.label(class="form-control-label") }} <br />
{{ form.opts }}<br />
<ul>
{% for error in form.opts.errors %}
<li style="color: red;">[{{ error }}]</li>
{% endfor %}
</ul>
</div>
</fieldset>
{{ form.submit(class="btn btn-outline-info border-bottom mb-2") }}
</form>
{% endblock %}
templates/index.html
{% extends "layouts.html" %}
{% block content %}
<div class="content-section">
<h1>Service Desk</h1>
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" href="{{ url_for('new_ticket') }}">New Ticket</a> <a class="nav-item nav-link" href="{{ url_for('index') }}">Home</a>
</div>
</div>
{% endblock %}
I’ve tried everything that I could find on how to get the form data. From the print command this is what is displayed:
Windows shell display
I’d like it to return the company name(Company A, Company B, Company C) value when pulled from the form.opts.data, and not the object name as well, if that makes sense. I hope I got it all in there.
Rick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.