IS_SET_DB using

32 views
Skip to first unread message

SanDiego

unread,
Apr 6, 2016, 1:04:47 AM4/6/16
to web2py-users

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.

Dave S

unread,
Apr 6, 2016, 1:37:02 AM4/6/16
to web...@googlegroups.com


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
 

SanDiego

unread,
Apr 6, 2016, 4:30:02 PM4/6/16
to web2py-users
Thank you very much. By the way, I realized that I sent out without completing the subject. :)


On Tuesday, April 5, 2016 at 10:37:02 PM UTC-7, Dave S wrote:


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.
Dropdown menu is created as expected. When viewing the page after saving it, the keys are displayed. I guess I need to write a function to return the T() function counterpart value for the view.  

 

My questions are:

  • Is there an easy way for retaining the order and displaying correctly?

This works good for the tuples case as you pointed out. 
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?

I couldn't find the help page. I searched the book both on the page (See attachments) and using Google but that is not listed. Is there anyway to improve the search feature? The material is sufficient. I should have gone through the forms/validators section more thoroughly. 

/dps
Thanks.
S/ 
search-01.PNG
search-02.PNG
Reply all
Reply to author
Forward
0 new messages