in your code, macaddr is a resultset of your query. Basically it's a list
of Rows objects, done like this
[ {'mac_addr' : value}, {'mac_addr':value1}, {'mac_addr' : value2}]
you should "reduce" the list of dictionaries to a list of values.
def get_macaddr():
# Grab distinct MAC addresses from DB
macaddr = db(db.wifiusage).select(db.wifiusage.mac_addr, distinct=True)
macaddr = [row.mac_addr for row in macaddr] #line added by Niphlod
form = SQLFORM.factory(
Field('macaddr', label='Select a MAC Address', requires=IS_IN_SET(
macaddr)))
return dict(form=form)
And everything should work ok.
On Thursday, October 18, 2012 1:02:44 AM UTC+2, Jason Jeremias wrote:
>
> So I'm trying out Web2py, I'm a complet newbie. I've been using python to
> write CGI for years, I have python cgi that I use to display data from
> mysql db. Its pretty simple and I thought I'd trying to recreate it using
> web2py. Basically its a simple html form that has a drop box and a submit
> button. The drop box is filled with mac addresses from a database. So
> basically you select the mac address, hit submit and it brings back all the
> data in the db related to that mac address.
>
> My current cgi program uses a simple function to grab the distinct mac
> addresses from the db and put them in to a dropdown.
>
> def PrintSelectMAC():
> db = MySQLdb.connect("localhost","myuser","mypass","wifi_usage")
> cursor = db.cursor()
> sql = 'SELECT DISTINCT(mac_addr) from wifi_usage'
> cursor.execute(sql)
> data = cursor.fetchall()
>
> print ("<b>Select MAC Addresses:</B><BR> \n")
> print ("<SELECT NAME=\"mac_addr\">")
>
> for row in sorted(data):
> macaddr = row[0]
> print ("<OPTION VALUE=\"%s\">%s") %(macaddr, macaddr)
>
> print ("</SELECT>")
>
>
> I'm trying to get just this far with Web2py and I'm running into a
> learning curve or something. Here's what I've done so far..
>
> in my Model (db.py) I have added..
>
> db = DAL("mysql://myuser:mypass@localhost/wifi_usage")
> db.define_table('wifiusage',
> Field('u_date', 'date'),
> Field('account_id', 'string', length=16),
> Field('mac_addr', 'string', length=20),
> Field('upstream', 'integer'),
> Field('downstream', 'integer'),
> Field('total', 'integer'),
> Field('location', 'string'),
> Field('package', 'string')
> )
>
> So this worked well. I then populated this database with data.
>
> in my controller (index) I added:
>
> def get_macaddr():
> # Grab distinct MAC addresses from DB
> macaddr = db(db.wifiusage).select(db.wifiusage.mac_addr,
> distinct=True)
>
> form = SQLFORM.factory(
> Field('macaddr', label='Select a MAC Address',
> requires=IS_IN_SET(macaddr)))
>
> return dict(form=form)
>
>
> I created a get_macaddr.html view. It looks like this.
> {{extend 'layout.html'}}
> <h1>This is the get_macaddr.html template</h1>
> {{=form}
>
>
> Here's the issue. My Form dropdown box. contains entries that look like
> this.
>
> <Row {'mac_addr': '0123456789ab'}>
>
> What I want is it to contain just the macaddress ''0123456789ab' for the
> value
>
> I have to admit the syntax is messing with me. I'm sure I'm doing
> something silly, Perhaps I've been doing cgi to long. I want to stick with
> it and learn web2py, what am I doing wrong?
>
> Thanks
>
>
--