尝试更新具有以下关系的记录时,我遇到了一些麻烦:
events_mentor_table = db.Table('events_mentors', db.Model.metadata,
db.Column('event_id', db.Integer, ForeignKey('event.id')),
db.Column('mentor_id', db.Integer, ForeignKey('mentor.id'))
)
class Mentor(db.Model):
__tablename__ = 'mentor'
id = db.Column(db.Integer(), primary_key=True)
first_name = db.Column(db.String())
last_name = db.Column(db.String())
job_title = db.Column(db.String())
company_url = db.Column(db.String())
twitter_url = db.Column(db.String())
linkedin_url = db.Column(db.String())
bio = db.Column(db.Text())
def __str__(self):
return '%s %s (%s)' % (self.first_name, self.last_name, self.job_title)
class Event(db.Model):
__tablename__ = 'event'
id = db.Column(db.Integer(), primary_key=True)
date = db.Column(db.Date())
start_time = db.Column(db.Time())
end_time = db.Column(db.Time())
title = db.Column(db.String())
address = db.Column(db.String())
description = db.Column(db.Text())
mentors = relationship('Mentor', secondary=events_mentor_table)
创建新条目时,我很简单:
@admin_events.route('/admin/events/create/', methods=['GET', 'POST'])
@login_required
def create_new():
form = EventForm()
if form.validate_on_submit():
event = Event(**form.data)
db.session.add(event)
db.session.commit()
flash('successfully added new event', 'success')
return render_template('admin/events_create_new.html', form=form)
正常工作。编辑时我使用以下内容:
@admin_events.route('/admin/events/<int:event_id>/', methods=['GET', 'POST'])
@login_required
def edit(event_id):
event = Event.query.filter_by(id=event_id)
form = EventForm(obj=event.one())
if form.validate_on_submit():
event.update(dict(**form.data))
db.session.commit()
flash("info updated", "success")
return render_template('admin/events_edit.html', event=event.one(), form=form)
但问题是我收到错误说明:
ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'Mentor'
[SQL: 'UPDATE event SET id = event_id AND id = mentor_id=%(param_1)s, date=%(date)s, start_time=%(start_time)s, end_time=%(end_time)s, title=%(title)s, address=%(address)s, description=%(description)s WHERE event.id = %(id_1)s'] [parameters: {'description': u'9', 'title': u'9', 'start_time': datetime.time(11, 11), 'param_1': [<app.models.Mentor object at 0x7f4227a6bf10>], 'id_1': 12, 'end_time': datetime.time(11, 11), 'address': u'9', 'date': datetime.date(2015, 7, 9)}]
我一直在摆弄一段时间,看起来无法解决究竟解决方案的问题。我很确定我可以清除mentor
数组,从表单数据中删除form.data.mentors
然后手动循环并提交,如:
mentors = form.data.mentors
del form.data.mentors
event.update(dict(**form.data))
event.mentors = mentors # this doesn't actually work, but I'm sure I could probably get it to work if I wanted to pursue this nasty solution.
答案 0 :(得分:8)
WTForms提供https://github.com/spring-projects/spring-security/tree/4.0.1.RELEASE/samples/aspectj-jc,用于此目的。
user nginx;
worker_processes 10;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 2024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 20;
gzip on;
include /etc/nginx/conf.d/*.conf;
}