Hey people.
Sorry I ask this question here, it is mainly concerned with
Toscawidgets, but also with Elixir and the interaction between the
two, so I though that this would be the best place to ask.
Suppose I have the setup below with Turbogears 1.1,
Elixir,Toscawidgets and Genshi.
Three model entities: Organization, Province and Country. An
organization has a province and a province has a country.
Now I want to setup one Organization form with toscawidgets, where
there are SingleSelectFields for country and province.
How do I ensure that these singleselectfields have the correct option
selected?
Overriding the __eq__ method on the Province Entity fixed the problem
for the province field.
So when I pass an existing Organization instance to the form, the name
textfield and province singleselectfield contain the correct values,
but the country singleselectfield does not.
And indeed, how should ToscaWidgets know that the
organization.location.country singleselectfield value should actually
be retrieved from the organization.location.province.country value in
the Entity.
And in the same line: How can I change the options available to a
widget in the Root.edit() method?
The thing is that I want the province field of the form to contain the
options (provinces) for the correct country.
I have setup the form with a CascadingSingleSelectField (tw.dynforms)
for country instead of a SingleSelectField, so that when the country
is changed in the form, the options for the Province field get
updated.
But how do I change the initial options in the field from inside the
controller?
Hope someone can help.
Cheers,
Dolf.
-----------------------------------------------
# file: model.py
from elixir import *
class Country(Entity):
name=Field(Unicode)
class Province(Entity):
name=Field(Unicode)
country=ManyToOne('Country')
def __eq__(self,other):
#yes this can be way more compact, I know
if type(other)==type(self) and other.id==self.id:
return True
elif other==self.name:
return True
elif type(other)==str or type(other)==int:
try:
if int(other)==self.id:
print self.id
print "same id"
return True
else:
return False
except ValueError:
return False
else:
return False
class Organization(Entity):
name=Field(Unicode)
province=ManyToOne('Province')
---------------------------------------------
#file: controller.py
from tw.forms import *
from model import *
class OrganizationWidgets(WidgetList):
name=TextField(validator=NotEmpty)
province=SingleSelectField(options=[(p.id,p.name) for p in
Province.query.all()])
country=SingleSelectField(options=[(c.id,c.name) for c in
Country.query.all()])
organizationform=TableForm(fields=OrganizationWidgets())
class Root(controllers.RootController):
@expose("someform")
def edit(self,id):
return {'data':Organization.get(id),'form':organizationform}
@expose()
@validate(addorganizationform)
@error_handler(edit)
def save(self,**kw):
return "whatever"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---