KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to have a select field filled with the results of a sqlalchemy request in a flask form. Here are the code : def possible_book(): return Book.query.with_entities(Book.id).all() class AuthorForm(Form): familyname = TextField('familyname', [validators.Required()]) firstname = TextField('firstname', [validators.Required()]) book_list = QuerySelectField('book_list',query_factory=possible_book,get_label='title',allow_blank=True) This is the template : <form action="" method="post" name="edit_author" enctype="multipart/form-data"> {{form.hidden_tag()}} <p>Veuillez donner son prénom (obligatoire) : {{form.firstname(value=author.firstname)}}</p> <p>Nom de famille (obligatoire) : {{form.familyname(value=author.familyname)}}</p> <p>Livre : {{form.book_list}}</p> <p><input type="submit" value="Envoyer"></p> </form> View : @app.route('/edit_author/<number>', methods = ['GET', 'POST']) def edit_author(number): if number == 'new': author = [] else: author = Author.query.filter_by(id = number).first() form = AuthorForm() if form.validate_on_submit(): a = Author(firstname= form.firstname.data, familyname= form.familyname.data) a.books = form.book_list.data db.session.add(a) db.session.commit() return redirect('/admin') return render_template('edit_author.html', form = form, author = author) The model (pasted only the associative table and the author table, much of the column are out of the question) author_book = db.Table('author_book', db.Column('author', db.Integer, db.ForeignKey('author.id')), db.Column('book', db.Integer, db.ForeignKey('book.id')) ) class Author(db.Model): id = db.Column(db.Integer, primary_key = True) firstname = db.Column(db.String(
Tags (comma-separated)
Save Edits
Cancel