Bottom Page

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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 96 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 48 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  got some problem with flask signup form. darktitan 1 215 Aug-30-2019, 06:05 AM
Last Post: aligoren
  Error object has no attribute text hcyeap 3 4,400 May-21-2019, 07:12 AM
Last Post: buran
  AttributeError: 'Response' object has no attribute 'replace' Truman 12 3,938 Mar-20-2019, 12:59 AM
Last Post: ichabod801
  AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) usman 0 989 Nov-20-2018, 09:50 PM
Last Post: usman
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 4,281 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  'ellipsis' object has no attribute 'lower' Bharath_AK 4 1,905 Mar-15-2018, 07:15 AM
Last Post: Bharath_AK
  AttributeError: 'NoneType' object has no attribute error vk7 1 3,130 Feb-02-2018, 08:36 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)