Reputation: 647
this is my template code:
if request.method == 'POST':
#------Invoice
#-----Invoice details
form = InvoiceReportEntries(request.form)
# items = []
for item in form.items:
lineitem = InvoiceLineItem(description=item.description,
amount=item.amount,
invoice_id=invoice.id)
db.session.add(lineitem)
db.session.commit()
My template is:
<form method="POST">
{{ form.hidden_tag() }}
<table>
<tr class="heading">
<td>
Item
</td>
<td>
Price
</td>
</tr>
<tr class="item">
<div>
<td>
<input id={{ items|length }} name="description-{{ items|length }}" required="" type="text" value="">
</td>
<td>
<input id={{ items|length }} name="amount-{{ items|length }}" required="" type="text" value="">
</td>
</div>
</tr>
It seems that my form on submit does not work well as I've this error:
sqlalchemy.exc.StatementError: (builtins.TypeError) float() argument must be a string or a number, not 'DecimalField' [SQL: INSERT INTO invoice_line_item (description, amount, invoice_id) VALUES (?, ?, ?)] [parameters: [{'description': '', 'invoice_id': 14, 'amount': <wtforms.fields.core.DecimalField object at 0x10bc52510>}]]
and data form for items are: {'items': [{'description': '', 'amount': None, 'id': '', 'csrf_token': ''}], 'csrf_token': 'xxxx'} But I d filled values in my template? description was 'test' and amount was 23
I will have more Thant one row in this table so I should identified each of them to save them in DB
Thanks for help
UPDATE: My models :
class InvoiceLineItem(db.Model):
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String, nullable=False)
amount = db.Column(db.Float, nullable=False)
invoice_id = db.Column(db.Integer, db.ForeignKey('invoice.id'), nullable=False)
def __init__(self, description, amount, invoice_id):
self.description = description
self.amount = amount
self.invoice_id = invoice_id
my forms.py:
class InvoiceitemForm(Form):
description = StringField('Description', validators=[DataRequired()])
amount = DecimalField('Amount', validators=[DataRequired()])
id = HiddenField('id')
class InvoiceReportEntries(Form):
items = FieldList(FormField(InvoiceitemForm), min_entries=1)
Upvotes: 1
Views: 202
Reputation: 7646
Just I quick advice, so you should've add also the wtforms forms.py and the 'Models.py.'
Hanving sayd that the error you're receiving is quite explicit:
sqlalchemy.exc.StatementError: (builtins.TypeError) float() argument must be a string or a number, not 'DecimalField'
What is referring to?
Well as I said I can fully determined why because some snaps of the code is missing, however the error codes speaks by itself:
amount': <wtforms.fields.core.DecimalField object at 0x10bc52510>
This is because you are calling the Class but not the instance of the class. In fact I guess you have in the forms.py the 'DecimalField', for more info --_> Here.
I can then see the HTML, there isn't the WTFORMS jinja tag but a standar HTML Tag input, this:
<input id={{ items|length }} name="amount-{{ items|length }}" required="" type="text" value="">
So I suggest to remove this and add the following:
{{ form.amount.label }}
{{ form.amount(class='yourclass') }}
Instead of the required="", add in the forms.py:
InputRequired() or DataRequired()
Upvotes: 1