从sqlalchemy查询返回返回属性错误:“ Nonetype”对象

时间:2020-05-19 12:14:30

标签: python flask-sqlalchemy flask-wtforms flask-login

在我的应用程序中,我希望登录用户能够安排与工作人员的约会。因此,我制作了wtform,并使其choices选项等于函数返回的列表。

这是代码

class ScheduleForm(FlaskForm):
    def add():
        addresses = Address.query.filter_by(user_id=current_user.id).all()
        choices = []
        for address in addresses:
            data = tuple((address.id, address.address))
            choices.append(data)
        return choices
    location = SelectField('Select your preferred address', choices=add())
    date = DateTimeField('Date and time of appointment', validators=[DataRequired()], format='%d-%m-%Y %H:%M')
    submit = SubmitField('Schedule Appointment')

但是,当我启动服务器时,我收到此错误

File "C:\Users\ANOINTING\Documents\Flask_projects\JobFair\JobFair\forms.py", line 112, in <module>
    class ScheduleForm(FlaskForm):
  File "C:\Users\ANOINTING\Documents\Flask_projects\JobFair\JobFair\forms.py", line 121, in ScheduleForm
    location = SelectField('Select your preferred address', choices=add())
  File "C:\Users\ANOINTING\Documents\Flask_projects\JobFair\JobFair\forms.py", line 114, in add
    addresses = Address.query.filter_by(user_id=current_user.id).all()
  File "C:\Users\ANOINTING\AppData\Local\Programs\Python\Python38-32\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'NoneType' object has no attribute 'id'

我的地址sqlalchemy模型是:

class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    address = db.Column(db.String(120), nullable=False)
    city = db.Column(db.String(15), nullable=False)
    landmark = db.Column(db.String(120), nullable=True)
    job = db.relationship('JobLog', backref='job_address', lazy=True)

0 个答案:

没有答案