Python 检查Flask应用程序中的数据库中是否存在所有三个WTForm
我如何编写一个验证函数来检查上面的db查询,如果所有三个数据都存在于db中,那么应该向用户抛出一个错误。Python 检查Flask应用程序中的数据库中是否存在所有三个WTForm,python,flask,sqlalchemy,flask-wtforms,Python,Flask,Sqlalchemy,Flask Wtforms,我如何编写一个验证函数来检查上面的db查询,如果所有三个数据都存在于db中,那么应该向用户抛出一个错误。 不确定如何进行验证,因为def validate\u days(self,days):只能检查该值是否存在于db中这几乎是正确的,但您需要参考self上的值并返回真/假 class TimeSheet(FlaskForm): days = SelectField('Please Select a Date', choices=[('1', '1'),
不确定如何进行验证,因为
def validate\u days(self,days):
只能检查该值是否存在于db中这几乎是正确的,但您需要参考self
上的值并返回真/假
class TimeSheet(FlaskForm):
days = SelectField('Please Select a Date', choices=[('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10'),
('11', '11'),
('12', '12'),
('13', '13'),
('14', '14'),
('15', '15'),
('16', '16'),
('17', '17'),
('18', '18'),
('19', '19'),
('20', '20'),
('21', '21'),
('22', '22'),
('23', '23'),
('24', '24'),
('25', '25'),
('26', '26'),
('27', '27'),
('28', '28'),
('29', '29'),
('30', '30'),
('31', '31')])
months = SelectField('Please Select a Month', choices=[('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10'),
('11', '11'),
('12', '12')])
year = SelectField('Please Select a Year', choices=[('2018', '2018'),
('2019', '2019'),
('2020', '2020'),
('2021', '2021'),
('2022', '2022')])
submit = SubmitField('Submit')
def validate(self, days,months,year):
d = Timesheet.query.filter_by(
user_id=current_user.id, days=days.data,months=months.data,years=year.data).all()
if d:
raise ValidationError(
'Date has already been selected')
为什么要投否决票?至少请给我链接,如果它已经回答
def validate(self):
rv = FlaskForm.validate(self)
if not rv:
return False
d = Timesheet.query.filter_by(user_id=current_user.id,
days=self.days.data,
months=self.months.data,
years=self.year.data).all()
if d:
self.days.errors.append('Date has already been selected')
return False
return True