これは私のテンプレートコードです:
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()
私のテンプレートは次のとおりです。
<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>
このエラーが発生したため、送信時のフォームがうまく機能しないようです。
sqlalchemy.exc.StatementError:(builtins.TypeError)float()引数は、 'DecimalField'ではなく文字列または数値である必要があります[SQL:INSERT INTO invoice_line_item(description、amount、invoice_id)VALUES(?、?、?)] [パラメータ:[{'description': ''、 'invoice_id':14、 'amount':<wtforms.fields.core.DecimalField object at 0x10bc52510>}]]
アイテムのデータフォームは次のとおりです。{'items':[{'description': ''、 'amount':None、 'id': ''、 'csrf_token': ''}]、 'csrf_token': 'xxxx' }しかし、テンプレートに値を入力しましたか?説明は「テスト」で、金額は23でした
このテーブルにはさらにThant1行があるので、それぞれを識別してDBに保存する必要があります
手伝ってくれてありがとう
更新:私のモデル:
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
私の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)
簡単なアドバイスなので、wtformsのforms.pyと「Models.py」も追加する必要があります。
ハンビングは、あなたが受け取っているエラーは非常に明白であると言いました:
sqlalchemy.exc.StatementError: (builtins.TypeError) float() argument must be a string or a number, not 'DecimalField'
何を指しているのですか?
私が言ったように、コードのいくつかのスナップが欠落しているため、理由を完全に判断できますが、エラーコードはそれ自体で説明しています:
amount': <wtforms.fields.core.DecimalField object at 0x10bc52510>
これは、クラスを呼び出しているが、クラスのインスタンスを呼び出していないためです。実際、forms.pyに「DecimalField」が含まれていると思います。詳細については--_>ここを参照してください。
次に、HTMLが表示されます。WTFORMSjinjaタグではなく、標準のHTMLタグ入力があります。
<input id={{ items|length }} name="amount-{{ items|length }}" required="" type="text" value="">
したがって、これを削除して、以下を追加することをお勧めします。
{{ form.amount.label }}
{{ form.amount(class='yourclass') }}
required = ""の代わりに、forms.pyを追加します。
InputRequired()またはDataRequired()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加