什么导致了shell下flask_sqlalchemy的order_by命令返回内存地址,跟官网的例子不一样啊?

ccpython
  • 38

官网的例子http://flask-sqlalchemy.pocoo...

>>> User.query.order_by(User.username)
[<User u'admin'>, <User u'guest'>, <User u'peter'>]

我得到的返回值

>>>User.query.order_by(User.id)
<flask_sqlalchemy.BaseQuery object at 0x0000000004BE2278>
>>> User.query.order_by(User.name)
<flask_sqlalchemy.BaseQuery object at 0x0000000004C346A0>

这种问题应该从什么地方开始入手分析呢?谢谢~

class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True, index=True)
username = db.Column(db.String(64),unique=True, index=True)
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
password_hash = db.Column(db.String(128))
confirmed = db.Column(db.Boolean, default=False)
name = db.Column(db.String(64))
location = db.Column(db.String(64))
about_me = db.Column(db.Text)
member_since = db.Column(db.DateTime(), default=datetime.utcnow)
last_seen = db.Column(db.DateTime(), default=datetime.utcnow)

def __init__(self, **kwargs):
    super(User, self).__init__(**kwargs)
    #super()不但能找到基类方法,而且还为我们传进 self
    if self.role is None:
       if self.email == current_app.config['FLASKY_ADMIN']:
           self.role = Role.query.filter_by(permissions=0xff).first()
       if self.role is None:
            self.role = Role.query.filter_by(default=True).first()

def can(self, permissions):
    return self.role is not None and \
           (self.role.permissions & permissions) == permissions

def is_administrator(self):
    return self.can(Permission.ADMINISTER)

def ping(self):
    self.last_seen = datetime.utcnow()
    db.session.add(self)

def __repr__(self):
    return '<User %r>' % self.username

@property
def password(self):
    raise AttributeError('password is not a readable attibute')

@password.setter
def password(self, password):
    self.password_hash = generate_password_hash(password)

def verify_password(self, password):
    return check_password_hash(self.password_hash, password)

def generate_confirmation_token(self, expiration=3600):
    s = Serializer(current_app.config['SECRET_KEY'], expiration)
    return s.dumps({'confirm': self.id})

def confirm(self, token):
    s = Serializer(current_app.config['SECRET_KEY'])
    try:
        data = s.loads(token)
    except:
        return False
    if data.get('confirm') != self.id:
        return False
    self.confirmed = True
    db.session.add(self)
    return True

def generate_reset_token(self, expiration=3600):
    s = Serializer(current_app.config['SECRET_KEY'], expiration)
    return s.dumps({'reset': self.id})

def reset_password(self, token, new_password):
    s = Serializer(current_app.config['SECRET_KEY'])
    try:
        data = s.loads(token)
    except:
        return False
    if data.get('reset') != self.id:
        return False
    self.password = new_password
    db.session.add(self)
    return True
回复
阅读 3.4k
2 个回答
✓ 已被采纳

你的user是不是dynamic的懒加载方式? 如果是的话,那就.all()才可以获取到实际的内容:

>>> User.query.order_by(User.username).all()

我看到你上面的代码

def __repr__(self):
    return '<User %r>' % self.username

但是你根本没有将username在__init__方法里面赋值给self.username怎么显示呢?

宣传栏