# brand table:
db.define_table('brand', Field('name'))

# The car table with computed field:
db.define_table('car',
                         Field('brand', db.brand),
                         Field('model'),
                         Field('year', 'integer'),
                         Field('slug', notnull=True, compute=*get_slug*)
                        )

# That's the function to get the slug:
import re
def get_slug(r):
    marca = db(db.brand.id==r.brand).select().first()
    slug_regex = re.compile('[^\w-]')
    return slug_regex.sub('',brand.name+'-%(model)s-%(year)s' %r).lower()

The function above is returning almost what I need, eg:
'toyota-corolla-2010' , but I need to put the inserted id to the end as:
'toyota-corolla-2010-2 ( 2 is the inserted id)

I am inserting records via "appadmin/insert/db/car", How to get the id
inside the "get_slug" function scope?







2010/8/15 mdipierro <mdipie...@cs.depaul.edu>

> Sorry. I do not understand what you want to do. Can you provide an
> example of code and/or desired workflow?
>
> Massimo
>
> On Aug 15, 3:18 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> > But I cant access form in this case, cause I am doing that inside a
> model,
> > and inserting the record via appadmin.
> >
> > File "/home/bruno/web2py/applications/lojadecarro/models/carros.py", line
> > 20, in get_slug
> >     print form.vars.id
> > NameError: global name 'form' is not defined
> >
> > Gonna try with crud
> >
> > 2010/8/15 mdipierro <mdipie...@cs.depaul.edu>
> >
> >
> >
> > > form.vars.id contains the id or a newly inserted record.
> >
> > > On Aug 15, 2:58 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> > > > I Just can't figure out how to retrive the inserted id as Row object
> does
> > > > not have the 'id' key
> >
> > > > 2010/8/15 Bruno Rocha <rochacbr...@gmail.com>
> >
> > > > > I am trying to do everything in the lambda, without the need to def
> the
> > > > > function.
> >
> > > > > Tks..
> >
> > > > > 2010/8/15 mdipierro <mdipie...@cs.depaul.edu>
> >
> > > > > If you define a function you do not need the lambda
> >
> > > > >> Field('slug', notnull=True, compute=get_brand),
> >
> > > > >> On Aug 15, 2:45 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> > > > >> > Thank you Massimo! works very well. I just needed to change a
> little
> > > in
> > > > >> > order to get the brand.name instead brand.id in the slug
> >
> > > > >> > This:
> >
> > > > >> > def get_brand(r):
> > > > >> >     import re
> > > > >> >     brand = db(db.brand.id==r.brand).select().first()
> > > > >> >     slug_regex = re.compile('[^\w-]')
> > > > >> >     return slug_regex.sub('',brand.nome+'-%(model)s-%(year)s'
> > > > >> %r).lower()
> >
> > > > >> > and:
> >
> > > > >> > Field('slug', notnull=True, compute= lambda r: get_brand(r)),
> >
> > > > >> > to get: 'toyota-corolla-2010' as slug  ( I still wants to get
> the
> > > r.idat
> > > > >> > the end)
> >
> > > > >> > Tks.
> >
> > > > >> > 2010/8/15 mdipierro <mdipie...@cs.depaul.edu>
> >
> > > > >> > > import re
> > > > >> > > slug_regex = re.compile('[^\w-]')
> >
> > > > >> > > Field('slug',compute=lambda r:
> > > > >> slug_regex.sub('','%(brand)s-%(model)s-%
> > > > >> > > (year)s' %r).lower())
> >
> > > > >> > > On Aug 14, 11:12 pm, Bruno Rocha <rochacbr...@gmail.com>
> wrote:
> > > > >> > > > I need a page slug for the cars in my car shop,
> > > > >> > > > instead havinghttp://.../default/car/2,
> > > > >> > > > I wanthttp://..../default/car/carbrand-carmodel-caryear-id
> >
> > > > >> > > > so I defined a table:
> >
> > > > >> > > > db.define_table('car'
> > > > >> > > >                          Field('brand', db.brand),
> > > > >> > > >                          Field('model'),
> > > > >> > > >                          Field('year','integer'),
> > > > >> > > >                          Field('slug')
> > > > >> > > >                          )
> >
> > > > >> > > > but I do not want the user to fill the "slug" field, so I
> have
> > > to
> > > > >> define
> > > > >> > > > default value to the slug field, concatenating 'brand' +
> 'model'
> > > +
> > > > >> 'year'
> > > > >> > > +
> > > > >> > > > 'slug' joining with a '-',
> > > > >> > > > eg:
> > > > >> > > > id = 2
> > > > >> > > > brand = 'Toyota'
> > > > >> > > > model = 'Corolla'
> > > > >> > > > year = '2010'
> > > > >> > > > slug = 'toyota-corolla-2010-2'
> >
> > > > >> > > > how can I get this to be automaticaly filled when inserting?
> > > (just
> > > > >> like a
> > > > >> > > > trigger)
> >
> > > > >> > --
> >
> > > > >> >http://rochacbruno.com.br
> >
> > > > > --
> >
> > > > >http://rochacbruno.com.br
> >
> > > > --
> >
> > > >http://rochacbruno.com.br
> >
> > --
> >
> > http://rochacbruno.com.br
>



-- 

http://rochacbruno.com.br

Reply via email to