Python Forum

Full Version: form.populate_obj problem "object has no attribute translate"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am creating a flask app using flask-wtf, I am using sqlachemy and mysql data base. I have an add function (which adds data to the data base) and an edit function (which updates the data base), the Error "object has no attribute translate" was affecting both, here is the code for the add function.
@bp.route('/timers/addtimer', methods=('GET', 'POST')) 
@login_required
def addtimer():
    form = AddTimerForm()
    if form.validate_on_submit():
        timer = Timers(name=form.name.data, heatingcircuit_name=str(form.heatingcircuit_name.data),\
                           boilercircuit_name=str(form.boilercircuit_name.data),\
                           duration=form.duration.data)
        db.session.add(timer)
        db.session.commit()
        flash('Congratulations, you are have a registered a new time control!')
        return redirect(url_for('timers.timers'))
    return render_template('timers/addtimer.html', title= 'add timer control',
                              form=form)
and here is the code for the form.
class AddTimerForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired()])
    heating_circuit_id = QuerySelectField('Heatingcircuit_id', 
                    query_factory=lambda: Heatingcircuit.query.all(),
                    allow_blank=True, get_label='id')
    Boilercircuit_id = QuerySelectField('Boiler circuit', 
                    query_factory=lambda: Boilercircuit.query.all(),
                    allow_blank=True, get_label='id')
    duration = StringField('Duration in minutes', validators=[DataRequired()])
    submit = SubmitField('Register')
The "translate" error affects the "QuerySelectfield" only, all other fields are ok. to over come this error in the "addtimer" function I have added "str" in each data field, ie heatingcircuit_name=str(form.heatingcircuit_name.data) this has resolved the problem here, but when I want to edit the entry I get an error message, here is the code for the edittimer function;
@bp.route('/timers/edittimer', methods=['GET', 'POST']) 
@bp.route('/timers/edittimer/<int:id>', methods=('GET', 'POST')) 
@login_required
def edittimer(id):
    obj = Timers.query.get(id) or Timers()
    form = AddTimerForm(request.form, obj=obj)
    if form.validate_on_submit():
        form.populate_obj(obj)
        db.session.add(obj)
        db.session.commit()
        return redirect(url_for('timers.timers'))
    return render_template('timers/edittimer.html', title= 'edit timer control',
                              form=form, obj=obj)
the error message indicates that it is at the point "db.session.commit()" (the full error details can be added if needed) I believe that I need to be able to convert the "obj" into a string but I dont know how.
I need help to over come this problem otherwise I cannot update existing entries!
Warm regards
Paul
ps this was once a single function, but I have divided it up as not all the fields were being populated.