Python Forum
form.populate_obj problem "object has no attribute translate"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form.populate_obj problem "object has no attribute translate"
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix "'dict_values' object has no attribute 'inject_wsgi'" in session_transacti devid 0 1,138 Aug-13-2023, 07:52 AM
Last Post: devid
  Problem with searching over Beautiful Soap object Pavel_47 30 9,677 Jun-30-2022, 10:27 PM
Last Post: snippsat
  AttributeError: 'ellipsis' object has no attribute 'register_blueprint' Mechanicalpixelz 2 2,356 Dec-29-2021, 01:30 AM
Last Post: Mechanicalpixelz
  AttributeError: ResultSet object has no attribute 'get_text' KatMac 1 4,336 May-07-2021, 05:32 PM
Last Post: snippsat
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,813 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  BeautifulSoup attribute problem zzy 3 2,966 Dec-07-2020, 11:07 PM
Last Post: zzy
  'NavigableString' object has no attribute 'h2' RandomCoder 5 5,310 May-20-2020, 09:01 AM
Last Post: Larz60+
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 10,385 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 3,020 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  got some problem with flask signup form. darktitan 1 2,215 Aug-30-2019, 06:05 AM
Last Post: aligoren

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020