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

not*_*you 4 python sqlalchemy jinja2 flask wtforms

我正在为代表账单的对象制作一个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)
Run Code Online (Sandbox Code Playgroud)

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

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

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Bill.date_due has an attribute 'strftime'
Run Code Online (Sandbox Code Playgroud)

现在,我已经深入了解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()])
Run Code Online (Sandbox Code Playgroud)

并且映射类也是:

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)
Run Code Online (Sandbox Code Playgroud)

Tei*_*man 5

我花了一些时间弄清楚你哪里出错了,但我想我发现了.这是你的代码:

@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)
Run Code Online (Sandbox Code Playgroud)

现在,如果obj在BillForm中将类作为kwarg传递,则表单会填充各种奇怪的对象.例如,如果我复制你做的并检查form.date_due.data,它说它是一个<sqlalchemy.orm.attributes.InstrumentedAttribute at 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)
Run Code Online (Sandbox Code Playgroud)

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