Re: [web2py] Re: simplest way to get rid of None strings
or, if indeed you want all strings fields with NULL values (backends side) or None (python side) to map to an empty string '', use none_value = None for f in db.table.fields: if db.table[f].type == 'string': db(db.table[f] == None).update(f='') On Sunday, January 19, 2014 3:21:49 AM UTC+1, Anthony wrote: but I mean the field is empty / Null / None (type), which is translated by web2py into a string value 'None'. None is translated into a string value as 'None' by Python, not web2py. If you want different behavior, you have to be explicit (no reason to assume the default behavior should be an empty string). You can achieve that by setting the represent attribute of the field: Field('myfield', represent=lambda v, r: '' if v is None else v) or more simply: Field('myfield', map_none='') Those methods will work in the grid and SQLFORMs, but not if you simply do {{=row.myfield}} somewhere. In that case, you would have to do something like {{=row.myfield or ''}}, or use the Rows.render() method to extract the row, which will automatically apply the represent function -- {{=rows.render(0).myfield}}. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: simplest way to get rid of None strings
thanks, I'll santinize the database onec a while. cheers, Stef On 19-01-14 13:45, Niphlod wrote: or, if indeed you want all strings fields with NULL values (backends side) or None (python side) to map to an empty string '', use none_value = None for f in db.table.fields: if db.table[f].type == 'string': db(db.table[f] == None).update(f='') On Sunday, January 19, 2014 3:21:49 AM UTC+1, Anthony wrote: but I mean the field is empty / Null / None (type), which is translated by web2py into a string value 'None'. None is translated into a string value as 'None' by Python, not web2py. If you want different behavior, you have to be explicit (no reason to assume the default behavior should be an empty string). You can achieve that by setting the represent attribute of the field: | Field('myfield',represent=lambdav,r:''ifv isNoneelsev) | or more simply: | Field('myfield',map_none='') | Those methods will work in the grid and SQLFORMs, but not if you simply do {{=row.myfield}} somewhere. In that case, you would have to do something like {{=row.myfield or ''}}, or use the Rows.render() method to extract the row, which will automatically apply the represent function -- {{=rows.render(0).myfield}}. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: simplest way to get rid of None strings
if you mean the other app fills the fields with an actual string that holds the 'None' value (it could be very well foo, bar or pizza) then fake_none_value = 'foo' for f in db.table.fields: db(db.table[f] == fake_none_value).update(f=None) should solve it On Saturday, January 18, 2014 5:12:36 PM UTC+1, aapaap wrote: hello, my database (sqlite) is (partially) filled by another program, which inserts Null or None values. Now if I use these fields in an HTML form, they show up as None instead of an empty string. Is there a simple way to solve this for all fields at once ? thanks, Stef -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: simplest way to get rid of None strings
thanks, it's a workaround, but I mean the field is empty / Null / None (type), which is translated by web2py into a string value 'None'. cheers, Stef On 18-01-14 19:06, Niphlod wrote: if you mean the other app fills the fields with an actual string that holds the 'None' value (it could be very well foo, bar or pizza) then fake_none_value = 'foo' for f in db.table.fields: db(db.table[f] == fake_none_value).update(f=None) should solve it On Saturday, January 18, 2014 5:12:36 PM UTC+1, aapaap wrote: hello, my database (sqlite) is (partially) filled by another program, which inserts Null or None values. Now if I use these fields in an HTML form, they show up as None instead of an empty string. Is there a simple way to solve this for all fields at once ? thanks, Stef -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: simplest way to get rid of None strings
but I mean the field is empty / Null / None (type), which is translated by web2py into a string value 'None'. None is translated into a string value as 'None' by Python, not web2py. If you want different behavior, you have to be explicit (no reason to assume the default behavior should be an empty string). You can achieve that by setting the represent attribute of the field: Field('myfield', represent=lambda v, r: '' if v is None else v) or more simply: Field('myfield', map_none='') Those methods will work in the grid and SQLFORMs, but not if you simply do {{=row.myfield}} somewhere. In that case, you would have to do something like {{=row.myfield or ''}}, or use the Rows.render() method to extract the row, which will automatically apply the represent function -- {{=rows.render(0).myfield}}. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.