Last active
March 4, 2016 18:16
Revisions
-
ja8zyjits revised this gist
Mar 1, 2016 . 1 changed file with 9 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,6 @@ ##Complex Models: class Profile(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) @@ -19,21 +12,22 @@ class Report(db.Model): profile_id = db.Column(db.Integer, db.ForeignKey(Profile.id)) disease_name = db.Column(db.String(50)) reported_date = db.Column(db.DateTime(), default=datetime.now()) ##The form: class ProfileForm(Form): name = StringField('Name', validators=[validators.Required()]) reports = FieldList(FormField(ReportForm), min_entries=2) class ReportForm(Form): disease_name = StringField('Disease Name') reported_date = DateField('Reported Date', default=date.today()) ##The view: def profile_page(): form = ProfileForm() if request.method == 'POST': @@ -45,6 +39,5 @@ def profile_page(): db.session.add(profile) db.session.commit() return render_template('profile_page.html', form=form) -
ja8zyjits created this gist
Mar 1, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ Have problems with wtforms populate_obj? Even I had few and this [hetsch gist](https://gist.github.com/hetsch/3922752) helped me to understand the working. Also this wtform google [mailing list](https://groups.google.com/forum/#!msg/wtforms/5KQvYdLFiKE/TSgHIxmsI8wJ) support of the same gist helped me realize what was I doing wrong. I also followed the second link and this statement _For that to work, the items must already exist in the list of the original object. In your case, your 'plugins' don't exist in your 'post' object, thus they cannot be updated_ gave me a better idea. check this out. This new idea was inspired from the gist but has no relation to the problem mentioned in hetsch gist. Basically populate_obj of the wtfform helps us to dynamicallly populate our model data. But there is a small issue with the FieldList and FormField, the model objects should exist. Check this out. Complex Models: ```python class Profile(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) reports = db.relationship( 'Report', backref='profile_of_report', lazy='dynamic') class Report(db.Model): id = db.Column(db.Integer, primary_key=True) profile_id = db.Column(db.Integer, db.ForeignKey(Profile.id)) disease_name = db.Column(db.String(50)) reported_date = db.Column(db.DateTime(), default=datetime.now()) ``` The form: ```python class ProfileForm(Form): name = StringField('Name', validators=[validators.Required()]) reports = FieldList(FormField(ReportForm), min_entries=2) class ReportForm(Form): disease_name = StringField('Disease Name') reported_date = DateField('Reported Date', default=date.today()) ``` The view: ```python def profile_page(): form = ProfileForm() if request.method == 'POST': if form.validate_on_submit(): profile = Profile() for _ in form.reports #we create the same number of object which we need to populate. profile.reports.append(Report()) form.populate_obj(profile) db.session.add(profile) db.session.commit() return render_template('profile_page.html', form=form) ``` Well it was true that you need an object to exist to pass a value, and now you can see how easy data population is with Wtfforms.