【发布时间】:2020-07-24 03:28:09
【问题描述】:
我有一个模型(公司),其中包含一些字段。创建公司记录后,我希望能够从表单更新此记录。但是,如果其中一个字段为空白,那么我想忽略该字段并保留当前信息。
我尝试创建一个form.py,然后在我的views.py 中从有效的表单中获取数据。但是,当我尝试提交数据时,出现错误:
AttributeError: 'scoped_session' 对象没有属性 'update'
我已经用谷歌搜索了这个,但我不知道为什么它不会更新。我已经搜索了过去一个小时左右,任何方向都会很棒。谢谢。
Models.py
class Company(db.Model, UserMixin):
__tablename__ = 'company'
id = db.Column(db.Integer, primary_key=True)
# The email is what connects Company and POC (Buyer)
email = db.Column(db.String(64), unique=True, index=True, nullable=False)
company = db.Column(db.String(64), index=True, nullable=False)
company_url = db.Column(db.String(64), index=True, nullable=False)
# Child relationship to the Event
duns = db.Column(db.String(11))
event_id = db.relationship(
'Events', secondary=event_company, backref='event', lazy='dynamic'
)
password_hash = db.Column(db.String(128))
buyer = db.relationship('Buyers', backref='buyer', lazy='dynamic')
def __init__(
self, email, company, duns, company_url,
password
):
Form.py
class UpdateCompanyForm(FlaskForm):
buyer_id = QuerySelectField(
'POC Name', query_factory=buyer_query, get_pk=get_pk
)
email = StringField('Email', validators=[Email()])
company = StringField('Company')
company_url = StringField('Company Website', validators=[URL(message='Must be a valid URL')])
duns = StringField('DUNS', validators=[DataRequired()])
password = PasswordField(
'Password', validators=[EqualTo('pass_confirm',
message='Passwords must match')]
)
pass_confirm = PasswordField('Confirm Password')
submit = SubmitField('Register!')
Views.py
class UpdateCompanyView(MethodView):
template_file = 'buyers/update_company.html'
form_class = UpdateCompanyForm
def get(self, comp_id):
# This id will autopopulate the company form as an object
company_id = Company.query.get(comp_id)
# Need to join this so I have all buyers associated with the company
buyer = Buyers.query.filter_by(company=comp_id).first()
return render_template(
self.template_file, form=self.form_class(obj=company_id),
buyer=buyer
)
def post(self, comp_id):
form = self.form_class()
if form.validate_on_submit():
buyers_list = form.buyer_id.data.id
company_info = Company(
email=form.email.data,
company=form.company.data,
company_url=form.company_url.data,
duns=form.duns.data,
password=form.password.data
)
db.session.update(company_info)
db.session.commit()
【问题讨论】:
标签: python-3.x flask flask-sqlalchemy