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

2012-08-01 19:15:53 +08:00
 paloalto
零基础,遇到一个技术问题,不会写了.....

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)



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

db.select(['users', 'profile'], where="users.id=profile.userID", limit = 10, order='id DESC')
paloalto
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
2012-08-03 11:10:56 +08:00
web.py的db api好像不能指定默认值,
你是指某个字段为空返回默认值吗,从mysql层面是可以用ifnull的
what="users.*, ifnull(avatarPath,'example@xx.com')"
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
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
2012-08-09 11:34:53 +08:00
sqlalchemy还是比较好用 在web.py里面import一下import sqlalchemy。
ipconfiger
2012-08-09 11:51:18 +08:00
sqlalchemy在建了一大堆关联后删数据才叫一个痛苦哦

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/44053

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX