使用datetime.date填充WTForms表单对象

使用datetime.date填充WTForms表单对象,第1张

概述我正在为代表账单的对象制作一个crud接口,如水费单,电费单等. 我正在使用sqlalchemy来处理数据,使用wtforms来处理表单,并使用flask来处理它. 以下是我的路线看起来像是用来编辑现有账单的表格: @app.route('/edit_bill/<int:bill_id>', methods = ['GET'])def edit_bill(bill_id): s = Se 我正在为代表账单的对象制作一个crud接口,如水费单,电费单等.

我正在使用sqlalchemy来处理数据,使用wtforms来处理表单,并使用flask来处理它.

以下是我的路线看起来像是用来编辑现有账单的表格:

@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)

使用wtforms,我将bill对象传递给BillForm构造函数,确保表示要编辑的帐单的数据填充到表单中.

这就是它窒息的地方.这是例外:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Bill.date_due has an attribute 'strftime'

现在,我已经深入了解python shell并查询了一个账单,以确保date_due上有一个datetime.date对象,就是这样.我使用Jinja来构建我的前端,所以我考虑创建一个模板过滤器,但我不知道它如何适用于wtforms,看起来sqlalchemy无论如何都是窒息.

那么它做什么?我非常有信心我只需要弄清楚如何将datetime.date对象转换为字符串,但我不知道如何去做.

HALP.谢谢!

编辑:这是BillForm类:

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()])

并且映射类也是:

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')>" % (self.name,self.pay_to,self.amount_due,self.date_due)
解决方法 我花了一些时间弄清楚你哪里出错了,但我想我发现了.这是你的代码:

@app.route('/edit_bill/<int:bill_ID>',form = form)

现在,如果在BillForm中传递一个类作为obj kwarg,那么表单将填充各种奇怪的对象.例如,如果我复制你所做的并检查form.date_due.data,它表示它是一个< sqlalchemy.orm.attributes.InstrumentedAttribute,位于0x277b2d0>宾语.与错误消息中所述的内容一样,此对象没有strftime属性.

因此,您的错误位于您提供的代码的第5行.如果要使用在第4行中检索到的帐单对象的详细信息填充表单,请将第5行替换为form = BillForm(obj = bill).正如您所看到的,“微妙”差异是账单中的小写字母b.我复制了你的代码,我相信应该解决问题.

如果您有兴趣,这就是我通常编辑视图的方式.

@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' and form.valIDate():        form.populate_obj(bill)        s.add(bill)        s.commit()        # Do some other stuff,for example set a flash()    return render_template('edit_bill.HTML',form = form)

我有一段时间没有使用sqlAlchemy所以我可能在那里犯了几个错误.希望这可以帮助!如果这回答你的问题’接受’答案.

总结

以上是内存溢出为你收集整理的使用datetime.date填充WTForms表单对象全部内容,希望文章能够帮你解决使用datetime.date填充WTForms表单对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064671.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存