represent list:reference

84 views
Skip to first unread message

Pepe Araya

unread,
Sep 11, 2012, 5:35:25 PM9/11/12
to
Hello,
I need to display the format value of the referenced table in a list:reference and not the list of ids.

I have these tables:
db.define_table('personas',
 Field('nombres'),
 Field('apellidos'),
 format='%(nombres)s %(apellidos)s')

db.define_table('publicaciones',
 
Field('titulo'),
 
Field('descripcion', 'text', label='Descripción'),
 Field('autores', 'list:reference db.personas',
        requires= IS_IN_DB(db, 'personas.id', '%(nombres)s %(apellidos)s', multiple=True)),
 format='%(titulo)s')

this controller function:
def filtro():
    tipo = request.args(0)
    lista_publicaciones = db(db.publicaciones.tipo == tipo).select(orderby =~ db.publicaciones.fecha)
    if lista_publicaciones:
        lista_publicaciones=lista_publicaciones
    else:
        lista_publicaciones = "0"
    return dict(lista_publicaciones=lista_publicaciones)

and this in the view:

{{if lista_publicaciones !='0':}}

            {{for publicacion in lista_publicaciones:}}
                    <div class="ez-fl bloque-filtro-66">
                        
                        <h2><a href="{{=URL('publicaciones','ver_publicacion', args=publicacion.id)}}" >{{=publicacion.titulo}}</a></h2>
                        Autores: {{=publicacion.autores}}

                        {{=XML(publicacion.descripcion)}}
                    </div>
                {{pass}}
            {{else:}}
                something...
            {{pass}}

using this in the view:
{{=db.publicaciones.autores.represent(publicacion.autores) }}

I get this error:
<type 'exceptions.TypeError'>('NoneType' object is not callable)

If I use:
Autores: {{=publicacion.autores}}

I get:
Autores: [5,66]

and I want:

Autores: nombres apellidos, nombres apellidos

Please, any help is welcome.
Thanks.

leonar...@gmail.com

unread,
Oct 21, 2016, 12:33:26 AM10/21/16
to web2py-users
Up.

Leonel Câmara

unread,
Oct 21, 2016, 2:40:30 PM10/21/16
to web2py-users
First you are getting the error because.
db.publicaciones.autores.represent is not defined so it is None and hence not callable.

That said. Here's a generic represent function you can use with list:reference fields.

def represent_listref(table, *fields):
   
def represent(values):
        result
= []
       
if values is None:
           
return None
       
for value in values:
            v
= db[table][value]
            rendered
= {'id': value}
           
for field in fields:
                represent
= db[table][field].represent
                rendered
[field] = represent(v[field]) if represent else v[field]
            result
.append(rendered)
       
return result
   
return represent



The way to use it is in the model set it as represent


db.define_table('publicaciones',
 
Field('titulo'),
 
Field('descripcion', 'text', label='Descripción'),
 
Field('autores', 'list:reference db.personas',

        requires
= IS_IN_DB(db, 'personas.id', '%(nombres)s %(apellidos)s', multiple=True), represent=represent_listref('personas', 'nombres', 'apellidos')),
 format
='%(titulo)s')

Then in your controller, you need to render the rows.

lista_publicaciones = list(db(db.publicaciones.tipo == tipo).select(orderby =~ db.publicaciones.fecha).render())

Now in your view for each "publicacione" in autores you will have a list of dicts with the fields nombres and apellidos as well as the id which my implementation of represent_listref always adds.




Reply all
Reply to author
Forward
0 new messages