Generating Select Fields in Flask-WTF From SQLAlchemy Queries (QuerySelectField)

preview_player
Показать описание
In this video I show you how to use the QuerySelectField to generate SelectFields in Flask-WTF. Using QuerySelectField allows you to use a SQLAlchemy query to create the select options.

Рекомендации по теме
Комментарии
Автор

I really like how you don't edit out issues you come across, lets me know (a beginner) errors will happen. It's also nice seeing someone work through the issue. Great video!

chrisjaurigue
Автор

Fantastic video as always. I tried a dozen other sources and this was the one that worked. Champion !

EdwardTilley
Автор

Thanks for the video!
I followed it to apply to my project. However, I had a "too many values to unpack" error. I solved it by changing the import
"from wtforms_sqlalchemy.fields import QuerySelectField "
to

"from wtforms.ext.sqlalchemy.fields import QuerySelectField"

Thanks again for your work !

CliMzOuu
Автор

I love you man! ❣ I've learnt alot from your videos.

jagdisho
Автор

bruh it worked just perfect for my project i only had two issues but i was able to solve following some comments in this video and solving by myself so thank you homie thumbs upp!!!!

mephystto
Автор

Very helpful video! Did anyone else wonder how to build a solution with an actual rendered search-field to select from in case the query output and therefore amount of choices get really big?

tomaszkowalczyk
Автор

thanks, buddy your tutorials are pretty much clear and very understandable so thanks for uploading this video thanks a lot

jefferywhite
Автор

Hi Anthony
How can we add a search bar choices field, say i have 20 options then i would like to search for an option rather than scrolling down
I have seen this in flask-admin idk how they do it

drac
Автор

Hey Anthony, again great video. I hope you continue!!
I have a question about your code. In the Models section of your code you put:

class Choice(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
extra = db.Column(db.String(50))

def __repr__(self):
return '[Choice {}]'.format(self.name)

However I have seen other code that instantiates the columns before the '__repr__' such as:

class Choice(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
extra = db.Column(db.String(50))

def __init__(self, name, extra):
self.name = name
self.extra = extra

def __repr__(self):
return '[Choice {}]'.format(self.name)

If you dont mind me asking, why is this not present in your code? Is it because of the structure?
Thanks for taking the time in responding to my post in advance.

chrisjaurigue
Автор

Has anyone experienced getting a 500 Internal Server error on the page thats using QuerySelectField? THe page will display if I create the table in the Sqlite3 db. But when I actually input data I receive the 500 error,

exceptionalopinion_
Автор

Hi Anthony, it works perfectly, but can you suggest me what can I do to remove duplicate values from the SelectField. With this even the duplicate ones are being populated. Thanks in advance!

Raghav
Автор

Is it possible to sort the items that are displayed in the QuerySelectField alphabetically? e.g. I'm pulling a bunch of names for selection and I'd like them ordered.

MartyMillerCrispe
Автор

So what I don't understand is, in what case we'd want to submit the __repr__ value ? In my opinion most cases there would be an "id" and a "name" column, you'd get the label from name and submit the ID as the value

1 | A
2 | B
3 | C

you select C from and click submit, it returns 3. I've spent over an hour trying to figure out how to do that. Still nothing.

rootifera
Автор

Thanks for your videos are super informative. I have more than a question, there are certain html that have choice box of countries, when it can select one. the module is pycountry. would it can use as a field on form. thanks again

dannisisgt
Автор

Thanks for the awesome videos!
I tried `QuerySelectMultipleField` and only problem seems to be ugly view which shows all options in a large area (like text box) and to select multiple, I should hold cmd/ctrl key.
Is this normal or I am missing something in view or other parts?

capturelife
Автор

Hello good afternoon, sorry for the inconvenience, I already have a QuerySelectField working on a form, and it works correctly to record a data from the list, but I want to use it to edit the information of a register, but I can not assign a value from a query, where I bring data that you previously registered. could you help me with it?

frenetic
Автор

Loving your flask videos, they are really helping me along with flask. I keep getting an error saying 'BaseQuery' object is not callable for my query_factory~ but if I add the .all() it just returns the results and not the query its self. Do you have any ideas about why this might be happening?

JesseBourretGheysen
Автор

Hi Thanks for tutorial. I have an Association Object so in the QuerySelectMultipleField i need the user to insert another value for every item selected and i don't know how to do that can you please help me?

patatasdelpapa
Автор

Thank you Anthony! You just saved my day! May God bless you abundantly!

andrewyang
Автор

Hello. Iam new to Python. Video is very educative. Can you share the source code to assist my understanding. Thanks.

computeradvantageslimited