SQLAlchemy/WTForms: set default selected value for QuerySelectField
Asked Answered
C

2

6

This [example][1] to set up a form with WTForms and SQLAlchemy in Flask and add a QuerySelectField to the form works. I am not using flask.ext.sqlalchemy, my code:

ContentForm = model_form(Content, base_class=Form)
ContentForm.author = QuerySelectField('Author', get_label="name")
myform = ContentForm(request.form, content)
myform.author.query = query_get_all(Authors)

Now I want to set the default value of the QuerySelectField's selectlist.

Tried passing a default kwarg in QuerySelectField and setting selected attributes. Nothing worked. Am I missing something obvious? Can someone help?

Callant answered 5/2, 2014 at 13:58 Comment(0)
C
5

You need to set your default keyword argument to the instance of Authors that you want to be the default:

# Hypothetically, let's say that the current user makes the most sense
# This is just an example, for the sake of the thing
user = Authors.get(current_user.id)
ContentForm.author = QuerySelectField('Author', get_label='name', default=user)

Alternately, you can provide the instance to the field on instantiation:

# The author keyword will only be checked if
# author is not in request.form or content
myform = ContentForm(request.form, obj=content, author=user)
Complexity answered 5/2, 2014 at 14:16 Comment(7)
The first method has no effect. The second causes a TypeError: 'ContentForm' object is not callable exception...Callant
@Callant - sorry, the second should have been for the instantiation of myform - updated. (As for the first, what happens if you use the id of the author ... I don't think it should work, looking at the code, but it can't hurt to try it)Complexity
It's strange, the only situation where the select box has the right default is myform = ContentForm(request.form, author=user) But then of course all other form fields are empty. Could this be a problem with Flask's requests?Callant
@Callant - I left out the obj= that I should have used for content - what we need to do is check in request.form, and if there is no content there, fill in the form with the data from obj (in this case content, and if content does not have an author then use the author we provide via keyword arguments. I have updated my second example and it should work now.Complexity
There was a bug somewhere else in my code. Your answer works.Callant
How do I get the value of selected QuerySelectField. For e.g. for my country select dropdown country = QuerySelectField(query_factory=Country.query.all, get_pk=lambda a: a.id, get_label=lambda a: a.name) I get the object <models.Country object at 0xaa1550c>. How do I get the value of country say 1, 2, 3, etc.Facilitate
Since data is going to be the actual instance you can just do your_field.data.id to get the ID and your_field.data.YOUR_FIELD_NAME to get the information in that field.Complexity
T
1

Try this:

ContentForm.author = QuerySelectField(
    'Author', 
    get_label="name", 
    default=lambda: Authors.get(current_user.id).one()
)
Thelen answered 18/7, 2018 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.