read a field of auth_user

101 views
Skip to first unread message

Aydin

unread,
Oct 7, 2016, 9:00:51 AM10/7/16
to web2py-users
This is a simple question but I couldn't find the answer for it.
How can I read a custom field in the auth_user?
I have added a new field to auth_user table by:
auth.settings.extra_fields['auth_user']= [
  Field('newfield','string')
]

now I want to read that value in the controller using db().select(...), but how to say to select() to choose that field of the auth user?

黄祥

unread,
Oct 7, 2016, 9:05:39 AM10/7/16
to web2py-users
pls try:
rows = db().select(db.auth_user.ALL)
for row in rows:
print row.newfield

if you want to explicit it :
rows = db().select(db.auth_user.first_name, db.auth_user.last_name, db.auth_user.newfield)
for row in rows:
print row.newfield

ref :

best regards,
stifan

Aydin

unread,
Oct 7, 2016, 9:24:21 AM10/7/16
to web2py-users
Thanks,

I do not want to read the newfield for all auth users but only for the current auth user.

黄祥

unread,
Oct 7, 2016, 9:29:27 AM10/7/16
to web2py-users
hm, not sure what do you mean with current auth user, is it current user login, if it yes, then you probably can try add the query for that
e.g.
query = (db.auth_user.id == auth.user_id)
rows = db().select(db.auth_user.ALL)
for row in rows:
print row.newfield

if you want to explicit the field :
query = (db.auth_user.id == auth.user_id)
rows = db().select(db.auth_user.first_name, db.auth_user.last_name, db.auth_user.newfield)
for row in rows:
print row.newfield

best regards,
stifan

Aydin

unread,
Oct 7, 2016, 9:30:36 AM10/7/16
to web2py-users
I tried rows = db().select(db.auth_user.id==auth.user.id).newfield but it did not work

黄祥

unread,
Oct 7, 2016, 9:36:27 AM10/7/16
to web2py-users
sorry my bad, forgot to insert the query on my code before
query = (db.auth_user.id == auth.user_id)
rows = db(query).select(db.auth_user.ALL)
for row in rows:
print row.newfield

if you want to explicit the field :
query = (db.auth_user.id == auth.user_id)
rows = db(query).select(db.auth_user.first_name, db.auth_user.last_name, db.auth_user.newfield)
for row in rows:
print row.newfield

best regards,
stifan
Message has been deleted

Marlysson Silva

unread,
Oct 7, 2016, 9:40:59 AM10/7/16
to web2py-users
Try:

information = auth.user.newfield

The auth.user contains a copy of auth_user table from current logged in user.

Or:

rows = db(db.auth_user.id==auth.user.id).select(db.auth_user.newfield)

Aydin

unread,
Oct 7, 2016, 9:48:45 AM10/7/16
to web2py-users
Thanks, information = auth.user.newfield  worked as expected.
I just want to mentioned when I used 
rows = db(db.auth_user.id==auth.user.id).select(db.auth_user.newfield)
it game me auth_user.newfielddata, data is the string stored in the newfield.

Marlysson Silva

unread,
Oct 7, 2016, 9:51:15 AM10/7/16
to web2py-users
Don't worked? 

Anthony

unread,
Oct 7, 2016, 10:35:33 AM10/7/16
to web2py-users
On Friday, October 7, 2016 at 9:48:45 AM UTC-4, Aydin wrote:
Thanks, information = auth.user.newfield  worked as expected.
I just want to mentioned when I used 
rows = db(db.auth_user.id==auth.user.id).select(db.auth_user.newfield)
it game me auth_user.newfielddata, data is the string stored in the newfield.

Not sure exactly what you mean, but to get the actual field value, do:

information = db(db.auth_user.id == auth.user.id).select(db.auth_user.newfield).first().newfield

Anyway, you really should use auth.user.newfield instead, as it does not hit the database.

Anthony

Aydin

unread,
Oct 7, 2016, 5:23:38 PM10/7/16
to web2py-users
The first one worked:information = auth.user.newfield
The second did not:rows = db(db.auth_user.id==auth.user.id).select(db.auth_user.newfield)

Aydin

unread,
Oct 7, 2016, 5:24:16 PM10/7/16
to web2py-users
Thanks Anthony, Absolutely worked fine.
Reply all
Reply to author
Forward
0 new messages