Wel... 
I solved my problem using a custom widget. I don't know if it is the best 
solution, but it works.
The records are now represented like below : 
*<root>
    fruits
        banana
        apple
    vegetables
        potatoes
        tomatoes*

Below my code for those who are interested :

*Create a "_widgets.py" file in models folder : *
class HierarchicalSelect(object):
    def __init__(self, db, title_field):
        self.options=[]
        self.db = db
        self.tablename = None
        self.fieldname = None
        self.title = title_field
        self.type = None
        self.parent=None
        self.rows=None

    def _childs_list(self, field, depth):
        path = XML("&nbsp;&nbsp;&nbsp;&nbsp;")*depth
        self.options.append((field['id'], path+field[self.title]))
        [self._childs_list(child, (depth+1)) for child in self.rows.find(
lambda row: row.parent == field.id)]   

    def widget(self, field, value):
        print str(field)
        self.tablename = field._table
        self.fieldname = field.name
        self.type = field.type
        self.rows = self.db(self.tablename).select()
        self.parent = field

        root_fields = self.db(self.parent==None).select()
        [self._childs_list(field,0) for field in self.rows.find(lambda row:row
.parent == None)] 

        opt=[OPTION(name, _value=key) for key,name in self.options]
        sel = SELECT(opt,_id="%s_%s" % (self.tablename, self.fieldname),
                        _class=self.type, 
                        _name=self.fieldname,
                        value=value)
        return sel


*In your model file . *
foodSelector = HierarchicalSelect(db,db.food.name)
db.food.parent.widget = foodSelector .widget



Le mardi 23 avril 2013 10:59:10 UTC+2, Loïc a écrit :
>
> Hi All
>
> I have a model file : 
>
> db.define_table('food',
>     Field('parent', 'reference food', label=T('Parent')),
>     Field('name', unique=True, notnull=True, label=T('Name')),
>     format='%(name)s'
> ) 
>
> Let's imagine I have some records in my db (fruits, vegetables, ...)
>
> In my controller, I create a form with
> form = crud.update(db.food,food,next=URL('default','index'))
>
> On my view, the "parent" field of the form is rendered as a "select" field 
> containing for example : 
> *fruits
> tomatoes
> banana
> apple
> potatoes
> vegetables*
>
> But I would like to see something like :
> *<root>
> |
> |___fruits
> |        |___banana
> |        |___apple
> |___vegetables
>          |___potatoes
>          |___tomatoes*
>
> I thought I could change the representation of my field using the 
> "represent" attribute 
> db.food.parent.represent = lambda v: ...
> But the representation of the field doesn't change in the form.
>
> I wonder if I'm in the right direction, or if there an other way to do 
> what I'm trying...?
>
> Thank you
>

-- 

--- 
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.


Reply via email to