Retrieving values from QuerySelectField and send to database – python

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.

New contributor

Rick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật