[web2py] Re: Passing values to widgets
That works perfectly. Thanks. Avik On Mar 27, 5:21 pm, "mr.freeze" wrote: > Sorry, low blood sugar. > > def units_widget(food_id): > def widget(f,v): > #build your widget > #using food_id > return widget > > db.ate.units.widget = units_widget(food_id) > > On Mar 27, 4:05 pm, Avik Basu wrote: > > > > > Also, if i wanted to extend SQLFORM.widgets.options.widget, how would > > i go about doing that? Is it as simple as replacing: > > > class Units_Widget(object): > > > with: > > > class Units_Widget(SQLFORM.widgets.options.widget): > > > Thanks, > > Avik > > > On Mar 27, 5:03 pm, Avik Basu wrote: > > > > I get the following error: > > > > TypeError: __init__() should return None, not 'function' > > > > I think the problem has to do with the following return statement in > > > the constructor: > > > > return widget > > > > Avik > > > > On Mar 27, 11:57 am, "mr.freeze" wrote: > > > > > Sorry, the constructor should be: > > > > > def __init__(*args,**kargs): > > > > > On Mar 27, 10:53 am, "mr.freeze" wrote: > > > > > > You need to make your widget a class so you can pass args to it in the > > > > > constructor: > > > > > > class Units_Widget(object): > > > > > def __init__(**args): > > > > > def widget(f,v): > > > > > #build your widget > > > > > #using args > > > > > return widget > > > > > > db.ate.units.widget = Units_Widget(food_id) > > > > > > On Mar 27, 10:42 am, Avik Basu wrote: > > > > > > > So, for my example, I tried your suggestion. I defined a widget in > > > > > > models/widgets.py: > > > > > > > def units_widget(field,value,food_id): > > > > > > ... > > > > > > > and then in the controller, I set the widget with: > > > > > > > db.ate.units.widget = units_widget(food_id) > > > > > > > But here I am not passing field and value so I get an error. What > > > > > > would be the proper way to call the widget constructor? > > > > > > > Avik > > > > > > > On Mar 27, 12:02 am, mdipierro wrote: > > > > > > > > in the controller you can do > > > > > > > > db.table.field.widget = SomeWidgetConstructor(args) > > > > > > > > Not sure if this is what you are asking. > > > > > > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > > > > > Is there a way to pass arguments to a field's widget from the > > > > > > > > controller? For example: > > > > > > > > > db.define_table("food", Field("name", "string'")) > > > > > > > > > db.define_table("units", > > > > > > > > Field("food_id", db.food), > > > > > > > > Field("name", "string") > > > > > > > > ) > > > > > > > > > db.define_table("ate", > > > > > > > > Field("food_id", db.food), > > > > > > > > Field("quantity", "double"), > > > > > > > > Field("units", "string", widget=units_widget), > > > > > > > > ) > > > > > > > > > In an update form for "ate", I would like to create a dropdown > > > > > > > > box of > > > > > > > > units that are associated with a given food_id. I realize > > > > > > > > could not > > > > > > > > be set in the db.py since the food id would be unknown in that > > > > > > > > scope, > > > > > > > > however the action in the controller would have access to the > > > > > > > > food id, > > > > > > > > but I would like to be able to pass that somehow to the widget. > > > > > > > > > Thanks, > > > > > > > > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
Sorry, low blood sugar. def units_widget(food_id): def widget(f,v): #build your widget #using food_id return widget db.ate.units.widget = units_widget(food_id) On Mar 27, 4:05 pm, Avik Basu wrote: > Also, if i wanted to extend SQLFORM.widgets.options.widget, how would > i go about doing that? Is it as simple as replacing: > > class Units_Widget(object): > > with: > > class Units_Widget(SQLFORM.widgets.options.widget): > > Thanks, > Avik > > On Mar 27, 5:03 pm, Avik Basu wrote: > > > I get the following error: > > > TypeError: __init__() should return None, not 'function' > > > I think the problem has to do with the following return statement in > > the constructor: > > > return widget > > > Avik > > > On Mar 27, 11:57 am, "mr.freeze" wrote: > > > > Sorry, the constructor should be: > > > > def __init__(*args,**kargs): > > > > On Mar 27, 10:53 am, "mr.freeze" wrote: > > > > > You need to make your widget a class so you can pass args to it in the > > > > constructor: > > > > > class Units_Widget(object): > > > > def __init__(**args): > > > > def widget(f,v): > > > > #build your widget > > > > #using args > > > > return widget > > > > > db.ate.units.widget = Units_Widget(food_id) > > > > > On Mar 27, 10:42 am, Avik Basu wrote: > > > > > > So, for my example, I tried your suggestion. I defined a widget in > > > > > models/widgets.py: > > > > > > def units_widget(field,value,food_id): > > > > > ... > > > > > > and then in the controller, I set the widget with: > > > > > > db.ate.units.widget = units_widget(food_id) > > > > > > But here I am not passing field and value so I get an error. What > > > > > would be the proper way to call the widget constructor? > > > > > > Avik > > > > > > On Mar 27, 12:02 am, mdipierro wrote: > > > > > > > in the controller you can do > > > > > > > db.table.field.widget = SomeWidgetConstructor(args) > > > > > > > Not sure if this is what you are asking. > > > > > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > > > > Is there a way to pass arguments to a field's widget from the > > > > > > > controller? For example: > > > > > > > > db.define_table("food", Field("name", "string'")) > > > > > > > > db.define_table("units", > > > > > > > Field("food_id", db.food), > > > > > > > Field("name", "string") > > > > > > > ) > > > > > > > > db.define_table("ate", > > > > > > > Field("food_id", db.food), > > > > > > > Field("quantity", "double"), > > > > > > > Field("units", "string", widget=units_widget), > > > > > > > ) > > > > > > > > In an update form for "ate", I would like to create a dropdown > > > > > > > box of > > > > > > > units that are associated with a given food_id. I realize could > > > > > > > not > > > > > > > be set in the db.py since the food id would be unknown in that > > > > > > > scope, > > > > > > > however the action in the controller would have access to the > > > > > > > food id, > > > > > > > but I would like to be able to pass that somehow to the widget. > > > > > > > > Thanks, > > > > > > > Avik > > -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
Also, if i wanted to extend SQLFORM.widgets.options.widget, how would i go about doing that? Is it as simple as replacing: class Units_Widget(object): with: class Units_Widget(SQLFORM.widgets.options.widget): Thanks, Avik On Mar 27, 5:03 pm, Avik Basu wrote: > I get the following error: > > TypeError: __init__() should return None, not 'function' > > I think the problem has to do with the following return statement in > the constructor: > > return widget > > Avik > > On Mar 27, 11:57 am, "mr.freeze" wrote: > > > > > Sorry, the constructor should be: > > > def __init__(*args,**kargs): > > > On Mar 27, 10:53 am, "mr.freeze" wrote: > > > > You need to make your widget a class so you can pass args to it in the > > > constructor: > > > > class Units_Widget(object): > > > def __init__(**args): > > > def widget(f,v): > > > #build your widget > > > #using args > > > return widget > > > > db.ate.units.widget = Units_Widget(food_id) > > > > On Mar 27, 10:42 am, Avik Basu wrote: > > > > > So, for my example, I tried your suggestion. I defined a widget in > > > > models/widgets.py: > > > > > def units_widget(field,value,food_id): > > > > ... > > > > > and then in the controller, I set the widget with: > > > > > db.ate.units.widget = units_widget(food_id) > > > > > But here I am not passing field and value so I get an error. What > > > > would be the proper way to call the widget constructor? > > > > > Avik > > > > > On Mar 27, 12:02 am, mdipierro wrote: > > > > > > in the controller you can do > > > > > > db.table.field.widget = SomeWidgetConstructor(args) > > > > > > Not sure if this is what you are asking. > > > > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > > > Is there a way to pass arguments to a field's widget from the > > > > > > controller? For example: > > > > > > > db.define_table("food", Field("name", "string'")) > > > > > > > db.define_table("units", > > > > > > Field("food_id", db.food), > > > > > > Field("name", "string") > > > > > > ) > > > > > > > db.define_table("ate", > > > > > > Field("food_id", db.food), > > > > > > Field("quantity", "double"), > > > > > > Field("units", "string", widget=units_widget), > > > > > > ) > > > > > > > In an update form for "ate", I would like to create a dropdown box > > > > > > of > > > > > > units that are associated with a given food_id. I realize could not > > > > > > be set in the db.py since the food id would be unknown in that > > > > > > scope, > > > > > > however the action in the controller would have access to the food > > > > > > id, > > > > > > but I would like to be able to pass that somehow to the widget. > > > > > > > Thanks, > > > > > > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
I get the following error: TypeError: __init__() should return None, not 'function' I think the problem has to do with the following return statement in the constructor: return widget Avik On Mar 27, 11:57 am, "mr.freeze" wrote: > Sorry, the constructor should be: > > def __init__(*args,**kargs): > > On Mar 27, 10:53 am, "mr.freeze" wrote: > > > > > You need to make your widget a class so you can pass args to it in the > > constructor: > > > class Units_Widget(object): > > def __init__(**args): > > def widget(f,v): > > #build your widget > > #using args > > return widget > > > db.ate.units.widget = Units_Widget(food_id) > > > On Mar 27, 10:42 am, Avik Basu wrote: > > > > So, for my example, I tried your suggestion. I defined a widget in > > > models/widgets.py: > > > > def units_widget(field,value,food_id): > > > ... > > > > and then in the controller, I set the widget with: > > > > db.ate.units.widget = units_widget(food_id) > > > > But here I am not passing field and value so I get an error. What > > > would be the proper way to call the widget constructor? > > > > Avik > > > > On Mar 27, 12:02 am, mdipierro wrote: > > > > > in the controller you can do > > > > > db.table.field.widget = SomeWidgetConstructor(args) > > > > > Not sure if this is what you are asking. > > > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > > Is there a way to pass arguments to a field's widget from the > > > > > controller? For example: > > > > > > db.define_table("food", Field("name", "string'")) > > > > > > db.define_table("units", > > > > > Field("food_id", db.food), > > > > > Field("name", "string") > > > > > ) > > > > > > db.define_table("ate", > > > > > Field("food_id", db.food), > > > > > Field("quantity", "double"), > > > > > Field("units", "string", widget=units_widget), > > > > > ) > > > > > > In an update form for "ate", I would like to create a dropdown box of > > > > > units that are associated with a given food_id. I realize could not > > > > > be set in the db.py since the food id would be unknown in that scope, > > > > > however the action in the controller would have access to the food id, > > > > > but I would like to be able to pass that somehow to the widget. > > > > > > Thanks, > > > > > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
Sorry, the constructor should be: def __init__(*args,**kargs): On Mar 27, 10:53 am, "mr.freeze" wrote: > You need to make your widget a class so you can pass args to it in the > constructor: > > class Units_Widget(object): > def __init__(**args): > def widget(f,v): > #build your widget > #using args > return widget > > db.ate.units.widget = Units_Widget(food_id) > > On Mar 27, 10:42 am, Avik Basu wrote: > > > So, for my example, I tried your suggestion. I defined a widget in > > models/widgets.py: > > > def units_widget(field,value,food_id): > > ... > > > and then in the controller, I set the widget with: > > > db.ate.units.widget = units_widget(food_id) > > > But here I am not passing field and value so I get an error. What > > would be the proper way to call the widget constructor? > > > Avik > > > On Mar 27, 12:02 am, mdipierro wrote: > > > > in the controller you can do > > > > db.table.field.widget = SomeWidgetConstructor(args) > > > > Not sure if this is what you are asking. > > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > Is there a way to pass arguments to a field's widget from the > > > > controller? For example: > > > > > db.define_table("food", Field("name", "string'")) > > > > > db.define_table("units", > > > > Field("food_id", db.food), > > > > Field("name", "string") > > > > ) > > > > > db.define_table("ate", > > > > Field("food_id", db.food), > > > > Field("quantity", "double"), > > > > Field("units", "string", widget=units_widget), > > > > ) > > > > > In an update form for "ate", I would like to create a dropdown box of > > > > units that are associated with a given food_id. I realize could not > > > > be set in the db.py since the food id would be unknown in that scope, > > > > however the action in the controller would have access to the food id, > > > > but I would like to be able to pass that somehow to the widget. > > > > > Thanks, > > > > Avik > > -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
You need to make your widget a class so you can pass args to it in the constructor: class Units_Widget(object): def __init__(**args): def widget(f,v): #build your widget #using args return widget db.ate.units.widget = Units_Widget(food_id) On Mar 27, 10:42 am, Avik Basu wrote: > So, for my example, I tried your suggestion. I defined a widget in > models/widgets.py: > > def units_widget(field,value,food_id): > ... > > and then in the controller, I set the widget with: > > db.ate.units.widget = units_widget(food_id) > > But here I am not passing field and value so I get an error. What > would be the proper way to call the widget constructor? > > Avik > > On Mar 27, 12:02 am, mdipierro wrote: > > > in the controller you can do > > > db.table.field.widget = SomeWidgetConstructor(args) > > > Not sure if this is what you are asking. > > > On 26 Mar, 18:26, Avik Basu wrote: > > > > Is there a way to pass arguments to a field's widget from the > > > controller? For example: > > > > db.define_table("food", Field("name", "string'")) > > > > db.define_table("units", > > > Field("food_id", db.food), > > > Field("name", "string") > > > ) > > > > db.define_table("ate", > > > Field("food_id", db.food), > > > Field("quantity", "double"), > > > Field("units", "string", widget=units_widget), > > > ) > > > > In an update form for "ate", I would like to create a dropdown box of > > > units that are associated with a given food_id. I realize could not > > > be set in the db.py since the food id would be unknown in that scope, > > > however the action in the controller would have access to the food id, > > > but I would like to be able to pass that somehow to the widget. > > > > Thanks, > > > Avik > > -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
So, for my example, I tried your suggestion. I defined a widget in models/widgets.py: def units_widget(field,value,food_id): ... and then in the controller, I set the widget with: db.ate.units.widget = units_widget(food_id) But here I am not passing field and value so I get an error. What would be the proper way to call the widget constructor? Avik On Mar 27, 12:02 am, mdipierro wrote: > in the controller you can do > > db.table.field.widget = SomeWidgetConstructor(args) > > Not sure if this is what you are asking. > > On 26 Mar, 18:26, Avik Basu wrote: > > > > > Is there a way to pass arguments to a field's widget from the > > controller? For example: > > > db.define_table("food", Field("name", "string'")) > > > db.define_table("units", > > Field("food_id", db.food), > > Field("name", "string") > > ) > > > db.define_table("ate", > > Field("food_id", db.food), > > Field("quantity", "double"), > > Field("units", "string", widget=units_widget), > > ) > > > In an update form for "ate", I would like to create a dropdown box of > > units that are associated with a given food_id. I realize could not > > be set in the db.py since the food id would be unknown in that scope, > > however the action in the controller would have access to the food id, > > but I would like to be able to pass that somehow to the widget. > > > Thanks, > > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
in the controller you can do db.table.field.widget = SomeWidgetConstructor(args) Not sure if this is what you are asking. On 26 Mar, 18:26, Avik Basu wrote: > Is there a way to pass arguments to a field's widget from the > controller? For example: > > db.define_table("food", Field("name", "string'")) > > db.define_table("units", > Field("food_id", db.food), > Field("name", "string") > ) > > db.define_table("ate", > Field("food_id", db.food), > Field("quantity", "double"), > Field("units", "string", widget=units_widget), > ) > > In an update form for "ate", I would like to create a dropdown box of > units that are associated with a given food_id. I realize could not > be set in the db.py since the food id would be unknown in that scope, > however the action in the controller would have access to the food id, > but I would like to be able to pass that somehow to the widget. > > Thanks, > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
[web2py] Re: Passing values to widgets
It depends on what specifically you are trying to accomplish. You could set the validator instead, for example: rr=db(db.food.name.like('m%')).select() db.ate.food_id.requires=IS_IN_SET([(r.id,r.name) for r in rr]) Denes On Mar 26, 6:26 pm, Avik Basu wrote: > Is there a way to pass arguments to a field's widget from the > controller? For example: > > db.define_table("food", Field("name", "string'")) > > db.define_table("units", > Field("food_id", db.food), > Field("name", "string") > ) > > db.define_table("ate", > Field("food_id", db.food), > Field("quantity", "double"), > Field("units", "string", widget=units_widget), > ) > > In an update form for "ate", I would like to create a dropdown box of > units that are associated with a given food_id. I realize could not > be set in the db.py since the food id would be unknown in that scope, > however the action in the controller would have access to the food id, > but I would like to be able to pass that somehow to the widget. > > Thanks, > Avik -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.