我正在尝试建立一个页面,客户可以在其中选择给定菜单项的副项和数量,并将其添加到订单中。我在尝试将其提交到我的数据库时遇到了麻烦...无论我尝试什么,都会抛出AttributeError,无论什么对象都没有属性'_sa'......
这是我的模特:
class MenuItems(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), index=True, unique=True)
category = db.Column(db.String(50))
price = db.Column(db.Float(6))
description = db.Column(db.String(140))
image_path = db.Column(db.String(140), unique=True)
def __unicode__(self):
return self.name
class Orders(db.Model):
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
created_on = db.Column(db.DateTime)
submitted = db.Column(db.Boolean)
submitted_on = db.Column(db.DateTime)
order_items = db.relationship('OrderItems', backref='orders', lazy='dynamic')
def __unicode__(self):
return str(self.customer_id)
class OrderItems(db.Model):
id = db.Column(db.Integer, primary_key=True)
order_id = db.Column(db.Integer, db.ForeignKey('orders.id'))
menu_item_id = db.Column(db.Integer, db.ForeignKey('menu_items.id'))
side_id = db.Column(db.Integer, db.ForeignKey('menu_items.id'))
quantity = db.Column(db.Integer)
menu_item = db.relationship(MenuItems, foreign_keys=[menu_item_id],
backref="menu_item")
side_item = db.relationship(MenuItems, foreign_keys=[side_id],
backref="side_item")
def __unicode__(self):
return str(self.id)
形式:
class OrderForm(Form):
side = SelectField(u'MenuItem', validators=[DataRequired()])
quantity = IntegerField(default=1)
submit = SubmitField("Add to Order")
def __init__(self, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
def validate(self):
if not Form.validate(self):
return False
相关观点:
@login_required
@davesbread.route('/detail/<menu_item_name>', methods=['GET', 'POST'])
def detail(menu_item_name):
form = OrderForm()
form.side.choices=[(s.id, s.name) for s in
MenuItems.query.filter_by(category='side').all()]
menu_item = MenuItems.query.filter_by(name=menu_item_name).first_or_404()
if form.validate_on_submit():
add_to_order(menu_item.id)
return render_template('customer/detail.html', title="Dave's Bread - " + menu_item.name,
menu_item=menu_item, form=form, user=current_user)
@login_required
@davesbread.route('/add_to_order/<menu_item_id>', methods=['GET', 'POST'])
def add_to_order(menu_item_id):
id = menu_item_id
form = OrderForm()
side = form.side.data
quantity = form.quantity.data
if current_user.is_anonymous:
return redirect(url_for('login'))
menu_item = MenuItems.query.filter_by(id=id).first()
order_exists = order_loader()
if order_exists:
order = Orders.query.filter_by(customer_id=current_user.id).first()
order_item = OrderItems(menu_item=menu_item, quantity=quantity,
side_item=side)
order.order_items.append(order_item)
db.session.add(order)
db.session.commit()
else:
order_item = OrderItems(menu_item=menu_item, quantity=quantity,
side_item=side)
order = Orders(customer_id=current_user.id,
created_on=datetime.datetime.now(),
submitted=False)
order.order_items.append(order_item)
db.session.add(order)
db.session.commit()
session['cart'] = True
flash('Item added to order')
return redirect(url_for('menu', category=menu_item.category))
模板:
{% extends "base.html" %}
{% block content %}
<table>
<tr>
<td>
<img src="{{ menu_item.image_path }}">
</td>
<td>
<h3>{{ menu_item.name }}</h3>
<br>
<p> {{ menu_item.description }}</p>
</td>
<td>
<form action="{{ url_for('add_to_order', menu_item_id=menu_item.id) }}"
method=post>
{{ form.hidden_tag() }}
{{ form.side }}
{{ form.quantity.label }}
{{ form.quantity }}
{{ form.submit }}
</form>
</td>
</tr>
<table>
{% endblock %}
这是追溯: https://gist.github.com/baccb79b31728e63f252
导致此错误的原因是什么?在此先感谢:)
答案 0 :(得分:1)
我认为问题就像描述here一样:你在一个需要对象的地方传递字符串作为构造函数的参数(即相应的字段是关系而不是字符串列)。
例如,这些行可以提供错误:
side = form.side.data
# here side is a string
# ...
order_item = OrderItems(menu_item=menu_item, quantity=quantity,
side_item=side)
# here side_item is expected to be an object
# as it is defined as a relationship in the model
您必须将side
中的side_item=side
替换为MenuItems
值所查询的相应side
实例。