Прежде всего, если я не ошибаюсь, я думаю, вы хотите передать details=form
, ваш объект формы.
В любом случае я не знаю метода all (), но у вас может быть что-то вроде ниже,
if form.validate_on_submit():
save_invoice_det(form) # pass your form object
и в рабочей функции вы можете иметь что-то, как показано ниже,
attrs = vars(form)
for attr in attrs:
if 'csrf_token' in attr:
continue
try:
print(f'for {attr} field data is {getattr(form, attr).data}')
except:
pass
В основном getattr(form, attr).data
- это ваше решение.
Редактировать: я использую Python 3.7 сделать настройки в соответствии с вашими потребностями
Редактировать: глядя на мой ответ, вы можете получить что-то вроде ниже,
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, DecimalField
from flask import Flask
from collections import OrderedDict
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
class CustomFlaskForm(FlaskForm):
ignore_field = ['csrf_token']
def all(self):
val = OrderedDict()
attrs = vars(self)
for attr in attrs:
if attr in self.ignore_field:
continue
try:
val[attr] = getattr(self, attr).data
except Exception as err:
pass
return val
class DetailForm(CustomFlaskForm):
def __init__(self):
super(DetailForm, self).__init__()
self.ignore_field.append('total')
item_code = IntegerField('Item Code')
item_desc = StringField('Description')
quantity = DecimalField('Qty')
price = DecimalField('Price', places=2)
total = DecimalField('Total', places=2)
with app.test_request_context() as app:
form = DetailForm()
print(form.all())
и вывод:
OrderedDict([('item_code', None), ('item_desc', None), ('quantity', None), ('price', None)])