单一模型在第一种形式下出现错误,但在第二种形式下起作用。 (AttributeError:'str'对象没有属性'name')

时间:2019-05-07 06:31:30

标签: flask flask-sqlalchemy flask-wtforms

我有一个“订单”模型和两个“订单表格”。一个用于管理员,另一个用于买方。但是一次只有一种形式可以正常工作,而另一种形式会抛出错误。

我的订单模型

class SampleOrder(db.Model):
    __tablename__ = 'sample_orders'

    id = db.Column(db.Integer,primary_key=True)
    date = db.Column(db.Date,nullable=False,default = datetime.date)
    code = db.Column(db.String,nullable=False,unique=True)
    item_code = db.Column(db.String,db.ForeignKey(Item.code))
    season_code = db.Column(db.String,db.ForeignKey(Season.code))
    qty = db.Column(db.Integer,nullable=False)
    unit_code = db.Column(db.String,db.ForeignKey(Unit.code))
    customer_name = db.Column(db.String,db.ForeignKey(Customer.name))

    def __init__(self,date,code,item_code,season_code,qty,unit_code,customer_name):
        self.date = date
        self.code = code
        self.item_code = item_code.code
        self.season_code = season_code.code
        self.qty = qty
        self.unit_code = unit_code.code
        self.customer_name = customer_name.name

我的客户表格

    def create_sample_order():
        title = "Create Sample Order"
        if 'email' in session:
            user = Customer.query.filter_by(email=session['email']).first()
            orders = SampleOrder.query.filter_by(customer_name=user.name).all()
            if user is not None:
                form = OrderForm(customer=user.name)
                if request.method == 'POST':
                    if form.validate() == False:
                        return 
    render_template('customer/forms/order_form.html',
                                           name=create_sample_order,
                                           user=user,
                                           title=title,
                                           form=form,
                                           )
                    else:
                        neworder = SampleOrder(
                            form.date.data,
                            form.code.data,
                            form.item.data,
                            form.season.data,
                            form.qty.data,
                            form.unit.data,
                           form.customer.data,
                        )
                        db.session.add(neworder)
                        db.session.commit()
                        return redirect(url_for('customer.sample_orders'))
                else:
                    return render_template('customer/forms/order_form.html',
                                       name=create_sample_order,
                                       form=form,
                                       title=title,
                                       user=user,
                                       )
            else:
                return redirect('customer.login')
        else:
           return redirect('index')

这将引发属性错误:'str'对象没有属性'name'.self.customer_name = customer_name.name

管理表单

from flask_wtf  import  Form
from wtforms   import  SubmitField,IntegerField,StringField,DateField
from wtforms.validators import DataRequired
from wtforms.ext.sqlalchemy.fields import QuerySelectField


from item.models.item import Season
from item.models.item import Item
from item.models.item import Unit

from vendor.models.vendor import Vendor
from customer.models.customer import Customer
from order.models.order import SampleOrder



def items():
    return Item.query.all()


def units():
    return Unit.query.all()


def seasons():
    return Season.query.all()


def customers():
    return Customer.query.all()


def vendors():
    return Vendor.query.all()


class OrderForm(Form):
    date = DateField('Date',validators=[DataRequired()])
    code = StringField('Order Code',validators=[DataRequired()])
    item = QuerySelectField('Item',query_factory=items,get_label='code', allow_blank=False,validators=[DataRequired()])
    season = QuerySelectField('Season',query_factory=seasons,get_label='code', allow_blank=False,validators=[DataRequired()])
    qty = IntegerField('Quantity',validators=[DataRequired()])
    unit = QuerySelectField('Units',query_factory=units,get_label='code', allow_blank=False,validators=[DataRequired()])
    customer = QuerySelectField('Customer',query_factory=customers,get_label='name', allow_blank=False,validators=[DataRequired()])
    submit = SubmitField('Add Order')

这很好用

0 个答案:

没有答案