python - Populate a WTForms form object with a datetime.date -
i'm cooking crud interface object representing bill, in water bill, electric bill, etc.
i'm using sqlalchemy handle data, wtforms handle forms, , flask serve it.
here's route looks serves form editing existing bill:
@app.route('/edit_bill/<int:bill_id>', methods = ['get']) def edit_bill(bill_id): s = session() bill = s.query(bill).filter_by(id=bill_id).first() form = billform(obj=bill) return render_template('edit_bill.html', form = form)
using wtforms, pass bill object billform constructor, ensuring data representing bill edited populated form.
this chokes. here's exception:
attributeerror: neither 'instrumentedattribute' object nor 'comparator' object associated bill.date_due has attribute 'strftime'
now, i've dipped python shell , queried bill make sure date_due has datetime.date
object on it, does. use jinja build front end, i've looked creating template filter, don't know how work wtforms, , looks sqlalchemy 1 choking anyway.
so do? i'm pretty confident need figure out how turn datetime.date
object string, i'm not sure how go that.
halp. thanks!
edit: here's billform class:
class billform(form): id = hiddenfield() name = textfield(u'name:', [validators.required()]) pay_to = textfield(u'pay to:',[validators.required()]) date_due = datefield(u'date due:',[validators.required()]) amount_due = integerfield(u'amount due:', [validators.required()]) date_late = datefield(u'late after:',[validators.required()]) amount_late = integerfield(u'late amount:', [validators.required()]) date_termination = datefield(u'termination date:',[validators.required()])
and mapping class, too:
class bill(base): __tablename__ = 'bills' id = column(integer, primary_key=true) name = column(string) pay_to = column(string) amount_due = column(integer) date_due = column(date) amount_late = column(integer) date_late = column(date) date_termination = column(date) def __init__(self, name, pay_to, amount_due, date_due, amount_late, date_late, date_termination): self.name = name self.pay_to = pay_to self.amount_due = amount_due self.date_due = date_due self.amount_late = amount_late self.date_late = date_late self.date_termination = date_termination def __repr__(self): return "<bill ('%s', '%s', '%s', '%s')>" % (self.name, self.pay_to, self.amount_due, self.date_due)
ah took me time figure out went wrong, think found out. here's code:
@app.route('/edit_bill/<int:bill_id>', methods = ['get']) def edit_bill(bill_id): s = session() bill = s.query(bill).filter_by(id=bill_id).first() form = billform(obj=bill) return render_template('edit_bill.html', form = form)
now, if pass class obj
kwarg in billform, form gets populated kinds of strange objects. example, if replicate did , inspect form.date_due.data
, says <sqlalchemy.orm.attributes.instrumentedattribute @ 0x277b2d0>
object. stated in error message, object not have strftime
attribute.
so, error in line 5 of code presented. if want populate form details of bill object retrieved in line 4, replace line 5 form = billform(obj=bill)
. can see, 'subtle' difference lowercase b in bill. replicated code , convinced should fix problem.
if you're interested, how make edit views.
@app.route('/edit_bill/<int:bill_id>', methods = ['get', 'post']) def edit_bill(bill_id): s = session() bill = s.query(bill).filter_by(id=bill_id).first() form = billform(request.form, obj=bill) if request.method == 'post' , form.validate(): form.populate_obj(bill) s.add(bill) s.commit() # other stuff, example set flash() return render_template('edit_bill.html', form = form)
i haven't used sqlalchemy while might have made couple of mistakes there. hope helps! if answers question 'accept' answer.
Comments
Post a Comment