У меня есть одна модель заказа и две формы заказа.один для администратора и другой для покупателя.Но только одна форма работает правильно, а другая выдает ошибку.
модель моего заказа
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')
это работает нормально