Are you trying to implement the Business Object pattern, Tom?


On Wed, Nov 14, 2012 at 12:54 PM, tom h <[email protected]> wrote:
> hi,
>
> thanks i'll take a look at the hasattr().
>
> that's not exactly what i mean.  the difference is having a model class that
> represents a real-world application, vs using the Table class to make
> database updates.  in your example there, Product(1) is getting a record
> set, and to insert, one needs to explicit work with the Table object.
>
> i'd like to create a class that represents one specific product, and have a
> easy way to save an instance of that class into the database.
>
> i've been reading this thread:
> https://groups.google.com/forum/?fromgroups=#!searchin/web2py/web2py$20vs$20World/web2py/KgzKryAEIGw/pTMR_ZuZoJwJ
>
> i think their conclusion at the end is fairly close to what i'm trying to
> do.  i was just wondering if there's a better way about it than how i've
> done it.
>
> thanks for your fast response.
>
> tom
>
> On Wednesday, November 14, 2012 7:01:34 AM UTC-5, viniciusban wrote:
>>
>> 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
>> >
>> > --
>> >
>> >
>> >
>
> --
>
>
>

-- 



Reply via email to