You may implement this getting your table fields and checking if your
class has this attribute with hasattr() (it's a Python builtin
function). It should be done in a upper level class, who you should
extend.
But, maybe you're missing that DAL does exactly what you mean: this
mapping between your database physical fields and your properties.
Maybe you could achieve that simply doing this:
>>> Product = db.define_table('product', ...) # all your define_table.
>From now on, you can do:
>>> p = Product(1)
>>> Product.price = 100
>>> Product.name = 'aaa'
>>> Product.update_or_insert()
--
Vinicius Assef
On Wed, Nov 14, 2012 at 6:09 AM, tom h <[email protected]> wrote:
> hi web2py gurus,
>
> just out of curiousity, i'd like to create a class (let's say Product) that
> represents a record in my table "product". right now, i can't figure out
> how to have the class attributes (p = Product(); p.price) saved to the
> database without internally having p.__fields_dict__ containing ONLY table
> fields, and then map the p.price to p.__fields_dict__['price'], and finally
> doing something like p.db['product'].insert(p.__fields_dict__).
>
> i'm just wondering if there's any better way to do this, sorry i'm pretty
> new to Python as well and probably am missing something.
>
> ideally, i'd like something that's more like:
>
> p = Product(id=1) or Product()
> p.random_var = 'abc'
> p.name = 'a'
> p.price = 100.0
> p.save() # or db['product'].update_or_insert(p)
>
> thanks a lot!
> tom
>
> --
>
>
>
--