I wanted to use translatable fields using T() function. I couldn't find any information for this in the document. I could only find the info from forum discussions. But still there are some problems:
COURSES = {'math': T('Math'), 'phy': T('Physics')}
...
Field(course, requires=IS_IN_SET(COURSES))
works. Only keys (e.g. 'phy') are stored in DB and COURSES dictionary can be used to display the value (e.g. COURSES['phy']). However, order is not retained.
Massimo recommends using sets to retain the order here:
COURSES = (('math', T('Math')), ('phy', T('Physics')))
...
Field(course, requires=IS_IN_SET(COURSES))
This time order is retained but then display will be problematic. Probably we can use list comprehension to display the translated value but not very intuitive.
My questions are:
I wanted to use translatable fields using T() function. I couldn't find any information for this in the document. I could only find the info from forum discussions. But still there are some problems:
COURSES = {'math': T('Math'), 'phy': T('Physics')}
...
Field(course, requires=IS_IN_SET(COURSES))
works. Only keys (e.g. 'phy') are stored in DB and COURSES dictionary can be used to display the value (e.g. COURSES['phy']). However, order is not retained.
Massimo recommends using sets to retain the order here:
COURSES = (('math', T('Math')), ('phy', T('Physics')))
...
Field(course, requires=IS_IN_SET(COURSES))
This time order is retained but then display will be problematic. Probably we can use list comprehension to display the translated value but not very intuitive.
My questions are:
- Is there an easy way for retaining the order and displaying correctly?
- How can we set the default value? e.g. if we set default to 'phy', I still want to show COURSES['phy'] in the dropdown menu in the form
- Would it be possible to add more info and examples for this function in the documentation? I think this is very basic function and used extensively.
On Tuesday, April 5, 2016 at 10:04:47 PM UTC-7, SanDiego wrote:I wanted to use translatable fields using T() function. I couldn't find any information for this in the document. I could only find the info from forum discussions. But still there are some problems:
COURSES = {'math': T('Math'), 'phy': T('Physics')}
...
Field(course, requires=IS_IN_SET(COURSES))
shouldn't you be setting the type to string? That's the default type, but it may have benefits to be explicit.works. Only keys (e.g. 'phy') are stored in DB and COURSES dictionary can be used to display the value (e.g. COURSES['phy']). However, order is not retained.
Massimo recommends using sets to retain the order here:
I think he's recommending a tuple of tuples. (I'm surprised it isn't a list of tuples, as in the book)then scroll down to IS_IN_SET().
COURSES = (('math', T('Math')), ('phy', T('Physics')))
...
Field(course, requires=IS_IN_SET(COURSES))
This time order is retained but then display will be problematic. Probably we can use list comprehension to display the translated value but not very intuitive.
Are you sure the drop down menu isn't created for you? The example in the book suggests that it is.
My questions are:
- Is there an easy way for retaining the order and displaying correctly?
see above
- How can we set the default value? e.g. if we set default to 'phy', I still want to show COURSES['phy'] in the dropdown menu in the form
would that be Field("Courses", "string", default=COURSES[1], requires=IS_IN_SET(COURSES)) ?
- Would it be possible to add more info and examples for this function in the documentation? I think this is very basic function and used extensively.
The material at the URL seems clear enough, although it doesn't show using the T() function. What do you suggest is missing?
/dps