如何组织下订单

时间:2019-03-25 15:33:44

标签: python flask sqlalchemy

因此,当我下订单时,它会为购物车中的每个商品创建一个新订单。假设我的购物车中有3个电话,那么它将作为3个订单进入数据库。如果要制作该怎么办,以便使这3部手机在“我的订单”中显示为1笔订单?

这是我的模特:

class Order(TimestampMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=False )
    phone_id = db.Column(db.Integer, db.ForeignKey('phone.id'),  
                         unique=False, nullable=True)
    accessory_id = db.Column(db.Integer, db.ForeignKey('accessory.id'),  
                             unique=False, nullable=True)

用于显示的代码:

@phones.route("/myorders", methods=['GET', 'POST'])
@login_required
def myorder():
    orders = Order.query.filter_by(user_id=current_user.get_id())
    return render_template('phones/myorder.html', orders=orders) 

和HTML:

{% extends "base.html" %}
{% block app_content %}
<div class="row">
<div class="container">
<div class="col-md-5">
  <div class="content-section">
    <table class="table table-striped">
      {% for order in orders %}
      <tr><th>{{order.id}} {{order.created_at}}</th>
      <tr>
      {% if order.phone %}
      <td>{{order.phone.brand.name}} {{order.phone.model}} </td> 
      {% endif %}
      {% if order.accessory %}
      <td>{{order.accessory.brand}} {{order.accessory.compability.platform}} {{order.accessory.type.type_of_accessory}} </td> 
      {% endif %}
      {% endfor %}
    </table>
  </div>
</div>
</div>
</div>
{% endblock %}

页面上的外观:

order

用于创建订单的代码:

@phones.route("/order/", methods=['GET', 'POST'])
@login_required
def order():
list_of_phones = session['cart']
list_of_accessories = session['cart2']
for pid in list_of_phones:
    phone = Phone.query.get(pid)
    phone.stock = phone.stock - 1
    order = Order(
        user_id = current_user.get_id(),
        phone_id = phone.id)
    db.session.add(order)
    db.session.commit()

for aid in list_of_accessories:
    accessory = Accessory.query.get(aid)
    accessory.stock = accessory.stock - 1
    order = Order(
        user_id = current_user.get_id(),
        accessory_id = accessory.id)
    db.session.add(order)
    db.session.commit()

session.pop('cart')
session.pop('cart2')
flash('Order was added successfully', 'success')
return redirect(url_for('phones.shopping_cart'))

更改后的区别:

enter image description here

我修改了显示所有订单的代码:

@phones.route("/myorders", methods=['GET', 'POST'])
@login_required
def myorder():
# orders = Order.query.filter_by(user_id=current_user.get_id())
orders = db.session.query(Order).filter_by(user_id = current_user.id).group_by(Order.order_id).all()
print(orders)
return render_template('phones/myorder.html', orders=orders) 

这是可行的,因为它仅显示一个order_id,但在另一侧仅显示一项,请参见下文:

enter image description here

1 个答案:

答案 0 :(得分:1)

我测试了此代码,它可以工作并通过随机生成的order_id将订单分组:

@phones.route("/order/", methods=['GET', 'POST'])
@login_required
def order():
    list_of_phones = session['cart']    
    random_string = random_generator()
    for pid in list_of_phones:
        phone = Phone.query.get(pid)
        phone.stock = phone.stock - 1
        order = Order(
            user_id = current_user.get_id(),
            phone_id = phone.id)
            order_id = random_string
        db.session.add(order)
        db.session.commit()

    session.pop('cart')
    flash('Order was added successfully', 'success')
    return redirect(url_for('phones.shopping_cart'))

def random_generator():
    size = 16
    chars = string.ascii_letters + string.digits 
    return ''.join(random.choice(chars) for _ in range(size))

在您的应用路径中:

@phones.route("/myorders", methods=['GET', 'POST'])
@login_required
def myorder():
    orders = db.session.query(Order).filter_by(
        user_id = current_user.id).all()

    return render_template('phones/myorder.html', 
        orders=orders) 

在模板中,您可以使用类似以下的内容:

{% for order_id, order in orders | groupby('order_id') %}
    {{ order_id }}
    {% for i in order %}
        <ul>
          {{ i.id }}
          {{ i.phone_name }}
        </ul>
    {% endfor %}
    <br>
{% endfor %}

将一些测试数据添加到数据库中,我得到了

abc

    1 phone1 
    2 phone2 
    3 phone3 


xyz

    4 phone4 
    5 phone5 
    6 phone6