AttributeError:'功能'对象没有属性'已确认'与sqlalchemy

时间:2016-02-29 18:02:34

标签: python flask sqlalchemy flask-sqlalchemy

我正在努力修改cookiecutter Flask应用。这使用flask_login usermixin。

我有一个方法寄存器,其中包含:

from myflaskapp.models.user import User

if form.validate_on_submit()
    new_user = User.query.filter_by(email=posted_email).first
    if new_user:
        if new_user.confirmed:

posted_email不在表中,所以我认为会返回None值。为什么这不起作用?

myflaskapp.models.user:

from flask.ext.login import UserMixin
from myflaskapp.extensions import db, bcrypt
from myflaskapp.database import (
    Column,
    Model,
    ReferenceCol,
    relationship,
    SurrogatePK,
)



class User(UserMixin, SurrogatePK, Model):

    __tablename__ = 'users'
    username = Column(db.String(80), nullable=True)
    email = Column(db.String(80), nullable=False)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    active = Column(db.Boolean(), default=False)
    admin = Column(db.Boolean(), default=False)
    confirmed = db.Column(db.Boolean, default=False)
    confirmed_on = db.Column(db.DateTime, nullable=True)

>>> User.query.filter_by(email=posted_email).first
<bound method BaseQuery.first of <flask_sqlalchemy.BaseQuery object at 0x03DF4FF0>>
>>> User.query.filter_by(email=posted_email)
<flask_sqlalchemy.BaseQuery object at 0x03DF4030>
>>> new_user = User.query.filter_by(email=posted_email).first
>>> type(new_user)
<type 'instancemethod'>

回溯:

Traceback (most recent call last):
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1836, in __call__
  return self.wsgi_app(environ, start_response)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1820, in wsgi_app
  response = self.make_response(self.handle_exception(e))
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1403, in handle_exception
  reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1817, in wsgi_app
  response = self.full_dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
  rv = self.handle_user_exception(e)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
  reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
  rv = self.dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_debugtoolbar\__init__.py", line 124, in dispatch_request
  return view_func(**req.view_args)
File "C:\envs\r2\mini\myflaskapp\views\public.py", line 73, in register
  if new_user.confirmed:

属性错误:&#39;功能&#39;对象没有属性&#39;已确认&#39;

1 个答案:

答案 0 :(得分:6)

您需要致电first(),这是一种方法:

void testFullAndGrow()
{
   struct Set *set = constructSet();
   int i = 0;

   for (i = 0; i < STARTING_SET_SIZE * 2; i++) {
     add(set, i);
   }
}