default_value has no effect for DropdownQuestion
rilshok opened this issue · 2 comments
Hello! I have encountered an issue with using the default_value parameter in the questions package. When using this parameter for DropdownQuestion
type questions, it does not apply and the default selected value is not displayed.
Here's a minimal code example that demonstrates this issue:
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from questions import DropdownQuestion, Form, FormPage
import uvicorn
class Page(Form):
person = DropdownQuestion(
title="Full name",
choices=["George Corbyn", "Stanley Castillo", "Ida Stanley"],
default_value="Stanley Castillo",
)
class Profile(Form):
page = FormPage(Page, title="Author")
def form():
html = Profile().render_html()
return HTMLResponse(html)
if __name__ == "__main__":
app = FastAPI()
app.get("/")(form)
uvicorn.run(app, host="0.0.0.0", port=8888)
In this example, the default_value has no effect, and the default selected value "Stanley Castillo" is not set in the dropdown list.
I have also tested this issue with other question types that inherit from ChoicesQuestion
, and they also do not apply the default_value.
I expected that when using the default_value parameter in the DropdownQuestion
definition, the default selected value would be displayed in the input field.
Env:
questions package version: 0.8.0
Python version: 3.11
Operating system: Linux
Thanks a lot for the report. I verified that this doesn't work at the moment. The python code correctly sets the property in the generated JSON, so it seems to be a problem on the SurveyJS side. I will look into it.
Thank you for your response and verification. I am confident that I have identified the cause: the overriding of the data field when creating the Survey.Model object in the survey_js.jquery.jinja template.
In your code, you use survey.data = data;, where data is likely an empty dictionary. This may be erasing default values for the questions. Judging from examples of survey form integration, overriding data is unnecessary. Simply passing a JSON dictionary to the model constructor, as shown in the SurveyJS documentation, should suffice.
Please consider removing survey.data = data;
from the template. This will likely resolve the issue with displaying default values.
Thank you for your attention to this matter.