Dynamic generation model

102 views
Skip to first unread message

Jose

unread,
Mar 11, 2011, 9:55:07 AM3/11/11
to web2py-users
Hi everyone,

I'm using PostgreSQL + postgis.

In web2py, in the model, define the table localidades:

tb_localidades = db.define_table('localidades',
Field('nombre', length=100),
...
Field('alias', length=50, comment='Un único nombre'),
...
Field('datos', 'upload', autodelete=True, label='Datos
(shapefile)', comment='Usar los tres archivos (.shp, .shx y .dbf)
zipeados'),
...
format='%(nombre)s'
)

Each row in localidades has an associated shapefile [1], which is
uploaded to the server. This shapefile is migrated to the database
with the command shp2pgsql (all this happens on the server).

alias field is used as the name of the table (with geographical
attributes) migrated.

I have 2 problems:

1) The only connection between the localidad and the geographical
table migrated, is the field alias. This somehow I could fix by
altering the table or sql that generates the table.

2) This is to me more difficult to solve. What I want is that the new
geographical table is included in the model dynamically. This table
has no fixed structure is defined by the shapefile associated with the
localidad.

Is there any way to include every geography table associated to the
localidad, within the model (dynamically)?

Jose


[1] http://en.wikipedia.org/wiki/Shapefile

Massimo Di Pierro

unread,
Mar 11, 2011, 10:39:10 AM3/11/11
to web2py-users
db.define_table('meta',Field('name'))
for row in db(db.meta).select():
db.define_table(row.name,Field('fieldname'))

def dynamically_add_new_table(db,name):
db.meta.insert(name=name)
db.define_table(name,Field('fieldname'))


then you call the function dynamically_add_new_table for the value of
name that you need.

Jose

unread,
Mar 14, 2011, 12:24:10 PM3/14/11
to web2py-users
Thanks Massimo.

In my case the table "meta" corresponds to "locations. "

What I posted works fine, but the question I have is how to create
fields using a loop. Perhaps a method add_field.

Best regards
Jose

Massimo Di Pierro

unread,
Mar 14, 2011, 3:23:24 PM3/14/11
to web2py-users
something like

db.define_table('name',*{Field(name) for name in ....])

Jose

unread,
Mar 14, 2011, 3:56:25 PM3/14/11
to web2py-users


On 14 mar, 16:23, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:
> something like
>
> db.define_table('name',*{Field(name) for name in ....])
>
> On Mar 14, 11:24 am, Jose <jjac...@gmail.com> wrote:
>
> > Thanks Massimo.

Exactly, that's what I need.

Thank you

toan75

unread,
Mar 15, 2011, 7:11:25 AM3/15/11
to web...@googlegroups.com
Hi Massimo Di Pierro,

How do get "requires", "defaut", "represent",... from field: db.mytable.myfield for create string:
"Field('myfield', requires=..., default=..., represent=...)" ?

Thanks.

Manuele Pesenti

unread,
Mar 15, 2011, 10:50:57 AM3/15/11
to web...@googlegroups.com, Jose
Dear Jose,

I think you posted a very interesting example... how have you resolved
the problem of the spatial support by web2py?
I mean the fields of the tables you are going to create dynamically are
the ones of a tipically spatial extension of a db... are they supported
by web2py DAL? Can you post the complete solution of how you have
implemented the dynamic tab inclusion in the model?

thank you very much

Cheers

Manuele

toan75

unread,
Mar 16, 2011, 10:23:38 AM3/16/11
to web...@googlegroups.com
Please help me in this case, thank very much.
Reply all
Reply to author
Forward
0 new messages