Populating WTForm FormField in a Flask view when object doesn't match form field names
up vote
0
down vote
favorite
I have a form setup like so:
class AddressForm(FlaskForm):
line1 = StringField()
city = StringField()
postcode = StringField()
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
And then I have a Flask view something like this:
@bp.route("/places/<ident>", methods=['GET', 'POST'])
def edit_place(ident):
place = api.get_place(ident)
form = PlaceForm(obj=place)
if form.validate_on_submit():
# do stuff with the form data
return render_template('place/edit.html', form=form)
The api.get_place(ident) returns data that doesn't match the shape of the field names in my Form classes, so my forms are always empty when rendered in the browser. For example, the response from the API might look like this:
{
"place": {
"place_name": "Foobar",
"address": {
"address1": "500 5th St",
"locality": "San Francisco",
"post_code": "90210"
}
}
}
How do I customize the code populates the PlaceForm with data when passing in obj?
python flask flask-wtforms wtforms
add a comment |
up vote
0
down vote
favorite
I have a form setup like so:
class AddressForm(FlaskForm):
line1 = StringField()
city = StringField()
postcode = StringField()
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
And then I have a Flask view something like this:
@bp.route("/places/<ident>", methods=['GET', 'POST'])
def edit_place(ident):
place = api.get_place(ident)
form = PlaceForm(obj=place)
if form.validate_on_submit():
# do stuff with the form data
return render_template('place/edit.html', form=form)
The api.get_place(ident) returns data that doesn't match the shape of the field names in my Form classes, so my forms are always empty when rendered in the browser. For example, the response from the API might look like this:
{
"place": {
"place_name": "Foobar",
"address": {
"address1": "500 5th St",
"locality": "San Francisco",
"post_code": "90210"
}
}
}
How do I customize the code populates the PlaceForm with data when passing in obj?
python flask flask-wtforms wtforms
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a form setup like so:
class AddressForm(FlaskForm):
line1 = StringField()
city = StringField()
postcode = StringField()
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
And then I have a Flask view something like this:
@bp.route("/places/<ident>", methods=['GET', 'POST'])
def edit_place(ident):
place = api.get_place(ident)
form = PlaceForm(obj=place)
if form.validate_on_submit():
# do stuff with the form data
return render_template('place/edit.html', form=form)
The api.get_place(ident) returns data that doesn't match the shape of the field names in my Form classes, so my forms are always empty when rendered in the browser. For example, the response from the API might look like this:
{
"place": {
"place_name": "Foobar",
"address": {
"address1": "500 5th St",
"locality": "San Francisco",
"post_code": "90210"
}
}
}
How do I customize the code populates the PlaceForm with data when passing in obj?
python flask flask-wtforms wtforms
I have a form setup like so:
class AddressForm(FlaskForm):
line1 = StringField()
city = StringField()
postcode = StringField()
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
And then I have a Flask view something like this:
@bp.route("/places/<ident>", methods=['GET', 'POST'])
def edit_place(ident):
place = api.get_place(ident)
form = PlaceForm(obj=place)
if form.validate_on_submit():
# do stuff with the form data
return render_template('place/edit.html', form=form)
The api.get_place(ident) returns data that doesn't match the shape of the field names in my Form classes, so my forms are always empty when rendered in the browser. For example, the response from the API might look like this:
{
"place": {
"place_name": "Foobar",
"address": {
"address1": "500 5th St",
"locality": "San Francisco",
"post_code": "90210"
}
}
}
How do I customize the code populates the PlaceForm with data when passing in obj?
python flask flask-wtforms wtforms
python flask flask-wtforms wtforms
asked Nov 20 at 0:31
magneticMonster
1,01142042
1,01142042
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I can't quite tell if this is actually the pattern you want. Since edit_place has both GET and POST methods do you really want to populate the form in both cases from the api.get_place() function, possibly overwriting the data from the request?
Anyway you might try something like the following:
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
def __init__(self, *kwargs):
super().__init__()
self.address, self.name = # some code to populate with data from kwargs
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I can't quite tell if this is actually the pattern you want. Since edit_place has both GET and POST methods do you really want to populate the form in both cases from the api.get_place() function, possibly overwriting the data from the request?
Anyway you might try something like the following:
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
def __init__(self, *kwargs):
super().__init__()
self.address, self.name = # some code to populate with data from kwargs
add a comment |
up vote
0
down vote
I can't quite tell if this is actually the pattern you want. Since edit_place has both GET and POST methods do you really want to populate the form in both cases from the api.get_place() function, possibly overwriting the data from the request?
Anyway you might try something like the following:
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
def __init__(self, *kwargs):
super().__init__()
self.address, self.name = # some code to populate with data from kwargs
add a comment |
up vote
0
down vote
up vote
0
down vote
I can't quite tell if this is actually the pattern you want. Since edit_place has both GET and POST methods do you really want to populate the form in both cases from the api.get_place() function, possibly overwriting the data from the request?
Anyway you might try something like the following:
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
def __init__(self, *kwargs):
super().__init__()
self.address, self.name = # some code to populate with data from kwargs
I can't quite tell if this is actually the pattern you want. Since edit_place has both GET and POST methods do you really want to populate the form in both cases from the api.get_place() function, possibly overwriting the data from the request?
Anyway you might try something like the following:
class PlaceForm(FlaskForm):
name = StringField()
address = FormField(AddressForm)
def __init__(self, *kwargs):
super().__init__()
self.address, self.name = # some code to populate with data from kwargs
answered Nov 20 at 21:36
Attack68
9291411
9291411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384566%2fpopulating-wtform-formfield-in-a-flask-view-when-object-doesnt-match-form-field%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown