broken backward compatibility for SQLFORM.widgets.checkboxes

9 views
Skip to first unread message

Wikus van de Merwe

unread,
Sep 20, 2010, 2:24:43 PM9/20/10
to web2py-users
I've noticed that recent versions of web2py (1.85) create a different
string in DB when form with checkboxes is serialised (compared to
version 1.77).

db.define_table("aaa", db.Field("bbb"), migrate=False)

db.aaa.bbb.widget = SQLFORM.widgets.checkboxes.widget
db.aaa.bbb.requires = [IS_IN_SET(BBB_VALUES, multiple=True),
IS_NOT_EMPTY()]

Before what was stored in DB was a '|' separated list of checked
options:
|xxx|yyy|zzz|
Now, it is a serialisation of a python list:
['xxx','yyy','zzz']

Obviously this breaks compatibility when I read the field from DB and
try to parse the options.

Interestingly setting default options on form display works "the old
way":
form=SQLFORM(db.aaa)
form.vars.bbb = "|".join(BBB_VALUES)

mdipierro

unread,
Sep 20, 2010, 2:36:12 PM9/20/10
to web2py-users
I checked and it works for me (insert is correct) but retrieval is not
correct because

db.define_table("aaa", db.Field("bbb"), migrate=False)

should be

db.define_table("aaa", db.Field("bbb","list:string"), migrate=False)

You should be able to change this and data in db should be file.
With "list:string" the '|xxx|yyy|zzz|' is in the db but web2py only
sees ['xxx','yyy','zzz']


On Sep 20, 1:24 pm, Wikus van de Merwe <dupakrop...@googlemail.com>
wrote:

Wikus van de Merwe

unread,
Sep 20, 2010, 3:01:23 PM9/20/10
to web2py-users
As far as I can see, when "list:string" is being used the effect is
very similar (I'm testing this on GAE). Again a list like
representation is used (this time using unicode):
[u'xxx',u'yyy',u'zzz'], not the old '|' separated strings. Does it
mean that now this is the new way of representing a list of checked
options or there is still a way to get strings split by '|' in the DB
as in the old versions of web2py? I would like to know exactly how it
is stored as I need to access this DB fields also outside of web2py.

mdipierro

unread,
Sep 20, 2010, 5:14:32 PM9/20/10
to web2py-users
On relational database with type="list:string" the internal
representation is the same as before '|xxx|yyy|zzz|' but it is no
longer exposed to the user. The user inserts a list a string and
extract a list of strings.

On GAE things have changed. At the web2py level the user still inserts
a list of strings and extract a list of strings but "list:string" is
mapped into a StringListPropery(). This allow taking advantage of fast
search.

"list:string" also handles pathological cases like

db.aaa.insert(bbb=['this','is','a|test']) and | gets escaped into
'||' as in '|this|is|a||test|'. Empty strings are not allowed.

and it allows better searching

db(db.aaa.bbb.contains('this')).select()

When using export/import it should export always (even on GAE) as a '|
xxx|yyy|zzz|'

Hope this clarifies it.


"list:string" the inter

On Sep 20, 2:01 pm, Wikus van de Merwe <dupakrop...@googlemail.com>
wrote:

Wikus van de Merwe

unread,
Sep 20, 2010, 7:07:24 PM9/20/10
to web2py-users
OK, I got it. On web2py level it is now a list of strings all the way.
That makes sense of course as it simplifies the usage. Internally its
'|' separated concatenation on RDBMS and list property on GAE, which
also makes sense.

What confused me was the fact that if you use simple "string" instead
of "list:string" with checkbox widget the internal representation on
GAE is a list like string "['xxx','yyy','zzz']". I guess an extra
sentence in Chapter 7 of the book (http://web2py.com/book/default/
chapter/07#Widgets) saying that checkboxes widget should be used with
"list:string" (not "string") should solve the problem.
Reply all
Reply to author
Forward
0 new messages