V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
paloalto
V2EX  ›  Python

求助web.py查询mysql,获得最新注册用户头像的问题。

  •  
  •   paloalto · 2012-08-01 19:15:53 +08:00 · 4079 次点击
    这是一个创建于 4257 天前的主题,其中的信息可能已经有所发展或是发生改变。
    零基础,遇到一个技术问题,不会写了.....

    mysql数据库中有一张users表,存了userID,username,nickname,email,password等注册信息;还有一张profile表,目前只存了userID和avatar_path两个字段。

    现在有一个页面,需要列出最新注册的10个用户的 昵称(nickname)和 头像(avatar_path).

    我的思路是这样:

    1:先得到最后注册的10个用户(model部分的代码如下):

    #——users.py——#
    def last_users():
    return db.select('users', limit = 10, order='id DESC')

    这样,controller部分就可以把last_users传给view,view可以获得用户的昵称(nickname)了:

    #——explore.py——#
    class explore:
    def GET(self):
    last_users = users.last_users()
    return view.base(view.explore(last_users), user, siteName)

    #——explore.html——#
    $def with (last_users)

    <h1>Explore</h1>
    <ul>
    $for user in last_users
    <li> $user.nickname </li>
    </ul>

    但是接下来取得用户头像就有点难了,我觉得首先需要获得最新注册的10个用户的userID,但我只会在view里$user.id。尝试在model部分得到userID吧:

    #——在users.py里加了以下代码——#

    def get_last_users_id():
    ids = web.listget(last_users(), 0, {})
    return ids.get('userID', False)

    上面这段代码只能得到最后注册的那一个用户的userID,因为.listget()里有一个参数为0,是取最后1个的。我郁闷半天,尝试去掉0、尝试改0为all,以得到全部的userID,结果报错说必须得有这么个参数。我不知道怎么才能获得这10个用户的userID。

    我是想获得了这10个用户的userID后,就可以把它们传给下面的get_avatar()去获取头像,但是这里我也不知道怎么处理了,我也没写过传一串userID去查询数据的经验,也许需要写个for循环什么的?

    def get_avatar_by_id(userID): #得到用户头像
    return web.listget(
    db.select('userProfile', vars=dict(id=userID),
    where='id = $id'), 0, {})
    def get_avatar(userID):
    avatar = get_avatar_by_id(userID)
    return avatar.get('avatarPath', False)



    请程序员们支招。。。
    6 条回复    1970-01-01 08:00:00 +08:00
    aggron
        1
    aggron  
       2012-08-02 10:26:09 +08:00   ❤️ 1
    返回多条数据时,用连接查询一次性返回结果集比较好吧, users和profile表~

    db.select(['users', 'profile'], where="users.id=profile.userID", limit = 10, order='id DESC')
    paloalto
        2
    paloalto  
    OP
       2012-08-02 23:47:00 +08:00
    @aggron 多谢解答!

    刚才执行时发现报错,是因为后面的order='id DESC'没有指明是用哪个表的id,改为

    db.select(['users', 'profile'], where="users.id=profile.userID", limit = 10, order='users.id DESC')

    这样就行啦!


    另外请问如果数据为空的话,db.select()能像web.listget()或者xxx.get()一样指定一个返回的数值吗?如下:

    web.listget(
    db.select('users', vars=dict(username=username),
    where='username = $username'), 0, {'为空时返回的东西'})
    ————

    return ids.get('id', False) —— False 也是为空时返回的。

    还是说db.select()需要手动去判断返回的值是否为空?
    aggron
        3
    aggron  
       2012-08-03 11:10:56 +08:00
    web.py的db api好像不能指定默认值,
    你是指某个字段为空返回默认值吗,从mysql层面是可以用ifnull的
    what="users.*, ifnull(avatarPath,'[email protected]')"
    db.select(['users', 'profile'], what=what, where="users.id=profile.userID", limit = 10, order='users.id DESC')
    加入what有个麻烦就是,如果要指定users表中的某个字段默认值,就不能使用通配符*了
    what="username,id,nick_name,......",
    ps:在sql中通常不使用select * from之类的通配符*,需要哪些字段就select 字段名,从这个编程习惯来说,也算个强制约束了。。
    memorybox
        4
    memorybox  
       2012-08-03 13:13:05 +08:00
    @paloalto
    web.py的db返回形式是[[{'a':'a'},{'b':'b'},{'c':'c'}...], [...]]这样的,如果没有查到记录,那默认就是[]了,我记得以前是这么用的,现在你可以查查手册。
    我以前是用
    res = db.select(...).list()
    try:
    return res[0]['a']
    except:
    #为空的处理

    用ids.get(...)也可以,不知道有没有更好的方法。
    ps:因为数据是直接传到模板里,我觉得在模板里也要对数据为空的时候做一下判断。
    parkman
        5
    parkman  
       2012-08-09 11:34:53 +08:00
    sqlalchemy还是比较好用 在web.py里面import一下import sqlalchemy。
    ipconfiger
        6
    ipconfiger  
       2012-08-09 11:51:18 +08:00
    sqlalchemy在建了一大堆关联后删数据才叫一个痛苦哦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3091 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:33 · PVG 22:33 · LAX 07:33 · JFK 10:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.