2013-05-21 43 views
3

我在做饭了污物接口的对象表示的纸币,如在水法案,电费等填充WTForms形式对象与datetime.date

我使用SQLAlchemy的至处理数据,通过wtforms处理表单,并通过烧瓶进行处理。

这里是我的路线是什么样子,供应形式为编辑现有的账单:

@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,我通过该法案的对象到BillForm构造,确保代表议案中的数据进行编辑它填充到表单中。

这是它呛的地方。这里的例外:

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

现在,我已经浸到蟒蛇外壳,并询问了一项法案,以确保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', '%s')>" % (self.name, self.pay_to, self.amount_due, self.date_due) 
+0

我们可以看看表单代码吗? –

回答

4

啊,我花了一些时间来找出你去错了,但认为我发现了它。这是你的代码:

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

现在,如果通过一类为BillForm的obj 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) 

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

+0

哦,我的天啊,我现在正在尖叫着在我的脑海里。你必须通过实例,而不是课堂! oi vey。我更喜欢你的编辑路线。我已经在rails中看到过这种模式(如果GET使用数据提供表单,如果POST从表单持久化数据),我完全会使用它。 谢谢! – nothankyou