我正在使用带有烧瓶的WTForms并将数据存储到Sql-Alchemy数据库。 Evrything似乎在正确的地方。我有一个models.py,forms.py,views.py和html文档,并没有引发任何错误。唯一的问题是,当我提交数据时,所有内容都会混淆,更确切地说,表单值会混淆并与其他内容交换。这是错误:
sqlalchemy.exc.StatementError
StatementError: SQLite DateTime type only accepts Python datetime and date objects
as input. (original cause: TypeError: SQLite DateTime type only accepts Python datetime
and date objects as input.) 'INSERT INTO menu (title, title_eng, alias, menu_type,
ordering, check_out_time, access, published, content, content_eng, image)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' [{'content': u'url/folder/image.jpg',
'title_eng': u'Home', 'title': u'\u0413\u043b\u0430\u0432\u043d\u0430\u044f',
'ordering': 1, 'menu_type': u'simple', 'content_eng': u'asd',
'access': datetime.date(1990, 1, 17), 'alias': u'home', 'image': u'asd',
'published': u'1', 'check_out_time': u'public'}]
然而,除了上面的陈述,必须有如下的声音:
sqlalchemy.exc.StatementError
StatementError: SQLite DateTime type only accepts Python datetime and date objects
as input. (original cause: TypeError: SQLite DateTime type only accepts Python datetime
and date objects as input.) 'INSERT INTO menu (title, title_eng, alias, menu_type,
ordering, check_out_time, access, published, content, content_eng, image)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' [{'content': u'asd',
'title_eng': u'Home', 'title': u'\u0413\u043b\u0430\u0432\u043d\u0430\u044f',
'ordering': 1, 'menu_type': u'simple', 'content_eng': u'asd',
'access': u'public', 'alias': u'home', 'image': u'url/folder/image.jpg',
'published': u'1', 'check_out_time': datetime.date(1990, 1, 17)}]
您可能已经注意到有4个变量可以转换数据。 内容和图片,另一个是访问和 check_out_time
你可以告诉我这是什么问题吗?这是我的 models.py:
class Menu(db.Model):
"""Menu is used for websites navigation titles.
eg. Home/About Us/Blog/Contacts/and etc"""
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(255))
title_eng = db.Column(db.String(255))
alias = db.Column(db.String(255))
menu_type = db.Column(db.String(10))
#menu type: simple, blog, gallery, contacts, products
ordering = db.Column(db.SmallInteger, default = '1')
check_out_time = db.Column(db.DateTime)
access = db.Column(db.String(30))
#access: user, reductor, manager, administrator
published = db.Column(db.SmallInteger, default = '1')
content = db.Column(db.String)
content_eng = db.Column(db.String)
image = db.Column(db.String(350))
def __init__(self, title, title_eng, alias,
menu_type, ordering, check_out_time, access,
published, content, content_eng,
image, metakey, metades):
self.title = title
self.title_eng = title_eng
self.alias = alias
self.menu_type = menu_type
self.ordering = ordering
self.check_out_time = check_out_time
self.access = access
self.published = published
self.content = content
self.content_eng = content_eng
self.image = image
self.metakey = metakey
self.metades = metades
这里是 views.py :
@admin.route('/manage/add_menu', methods = ['GET', 'POST'])
@login_required
def add_menu(parent = ''):
form = Add_menu_form()
if form.validate_on_submit():
new_menu = Menu(
form.title.data,
form.title_eng.data,
form.alias.data,
form.menu_type.data,
form.ordering.data,
form.access.data,
form.check_out_time.data,
form.published.data,
form.image.data,
form.content.data,
form.content_eng.data,
form.metakey.data,
form.metades.data)
db.session.add(new_menu)
db.session.commit()
flash('New menu was added successfully.')
return redirect(url_for('cabinet.manage', current = 'menu_settings'))
return render_template('admin/manage/site_figuration/add_menu.html',
title = 'Internet market',
parent = parent,
form = form)
这是我的 forms.py :
class Add_menu_form(Form):
"""Add_menu_form is used to add/edit menu"""
title = TextField('Title', [validators.Length(min=1, max=250), validators.Required()])
title_eng = TextField('Title in English', [validators.Length(min=1, max=250), validators.Required()])
alias = TextField('Alias')
ordering = IntegerField('Order')
check_out_time = DateField('Date of publication')
content = TextAreaField('Content', [validators.Required()])
content_eng = TextAreaField('Content in English', [validators.Required()] )
image = TextField('Heading image')
metakey = TextAreaField('Meta keywords')
metades = TextAreaField('Meta description')
menu_type = SelectField('Menu type',
choices=[('simple', u'обычное'),
('blog', u'блог'),
('products', u'продукция'),
('gallery', u'галерея')])
access = SelectField('Access',
choices=[('public', u'открытый'),
('registered', u'для зарегистрированных'),
('admin', u'для администратора')])
published = SelectField('Published',
choices=[('1', u'да'),
('0', u'нет')])
和html文档:
<form action="" method="post" name="add_menu">
<table class="table">
<tr>
{{ form.hidden_tag() }}
<td>Название меню</td>
<td>{{ form.title }}</td>
</tr>
<tr>
<td>Название меню на английском </td>
<td>{{ form.title_eng}}</td>
</tr>
<tr>
<td>Короткое название</td>
<td>{{ form.alias}}</td>
</tr>
<tr>
<td>Тип меню</td>
<td>{{form.menu_type}}</td>
</tr>
<tr>
<td>Позиция</td>
<td>{{form.ordering}}</td>
</tr>
<tr>
<td>Дата публикации</td>
<td>{{form.check_out_time}}</td>
</tr>
<tr>
<td>Доступ</td>
<td>{{form.access}}</td>
</tr>
<tr>
<td>Опубликовать</td>
<td>{{form.published}}</td>
</tr>
<tr>
<td>Заглавная картинка</td>
<td>{{form.image}}</td>
</tr>
<tr>
<td>Содержание</td>
<td>{{form.content}}</td>
</tr>
<tr>
<td>Содержание на английском</td>
<td>{{form.content_eng}}</td>
</tr>
<tr>
<td>HTML описание</td>
<td>{{form.metades}}</td>
</tr>
<tr>
<td>HTML ключевые слова </td>
<td>{{form.metakey}}</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="добавить меню" class="btn btn-default"/>
<a href="{{ url_for('cabinet.manage', current='menu_settings') }}" class="btn btn-default">cancel</a></td>
</tr>
</table>
</form>
答案 0 :(得分:3)
如果字段的名称相同(在Menu
和Add_menu_form
之间),则可以使用wtforms.Forms#populate_obj
method更新现有对象,而不是手动映射字段。从__init__
模型(Menu
)中删除自定义class Menu(db.Model)
方法,然后您就可以执行此操作:
if form.validate_on_submit():
new_menu = Menu()
form.populate_obj(new_menu)
db.session.add(new_menu)
db.session.commit()
答案 1 :(得分:1)
您需要检查/确认在new_menu变量中保留了排序。
换句话说,当您在new_menu
行中传递db.session.add(new_menu)
时,请确保商品的顺序相同。