Bruce,

Maybe I'm a little simple minded today ...

Could you give an example where you would have more than one model per 
class?

On Tuesday, May 15, 2012 3:43:33 PM UTC-4, Bruce Wade wrote:
>
> These examples are only good if you have 1 model per class. I prefer the 
> design used in Auth with a define_tables method.
>
> On Tue, May 15, 2012 at 12:41 PM, Bruno Rocha <rochacbr...@gmail.com>wrote:
>
>> Better example:
>>
>>
>> *# models/db.py*
>> ...
>> db = DAL(".....")
>> ...
>>
>> *# modules/datamodels/base.py*
>>
>> class BaseModel(object):
>>
>>     def define_table(self):
>>         self.db.define_table(self.tablename, *self.fields, **self.params)
>>
>> *# modules/datamodels/dog.py*
>>
>>
>> from gluon import current
>> from gluon.dal import Field
>> from datamodels.base import BaseModel
>>
>> class Dog(BaseModel):
>>     def __init__(self, db):
>>
>>         self.db = db
>>         self.T = current.T
>>         self.request = current.request
>>         self.tablename = "dog"
>>         
>>         self.fields = [
>>             Field("name", label=self.T("Dog name")),
>>             Field("guardian", "reference guardian"),
>>
>>             ....
>>         ]
>>
>>        self.params = dict(        
>>            migrate=True,
>>            format="%(name)s"
>>        )
>>
>>
>> *# modules/datamodels/guardian.py*
>>
>>
>> from gluon import current
>> from gluon.dal import Field
>> from datamodels.base import BaseModel
>>
>> class Guardian(BaseModel):
>>      def __init__(self, db):
>>           self.db = db
>>           self.tablename = "guardian"
>>           self.fields = [
>>               Field("name")
>>           ]
>>
>>          self.params = dict(migrate=True)
>>
>>
>> *# models/guardian/0.py
>>
>> *from datamodels.guardian import Guardian
>> guardian = Guardian(db)
>> guardian.define_table()
>>
>> *# models/dog/0.py*
>>
>> from datamodels.dog import Dog
>> from datamodels.guardian import Guardian
>>
>> # dogs does not have owners, they have guardians!
>> guardian = Guardian(db)
>> guardian.define_table()
>>
>> # dog have reference to guardian, so need to define it before
>> dog = Dog(db)
>> dog.define_table()
>>
>> *# controllers/guardian.py*
>>
>> def index():
>>      return db(db.guardian).select(0
>>
>> *# controller/dog.py*
>>
>> def index():
>>     return db(db.dog.guardian == db.guardian.id).select()
>>
>> It could be a little better, a singleton, a way to define references 
>> automatically etc...
>>
>>
>
>
> -- 
> -- 
> Regards,
> Bruce Wade
> http://ca.linkedin.com/in/brucelwade
> http://www.wadecybertech.com
> http://www.fittraineronline.com - Fitness Personal Trainers Online
> http://www.warplydesigned.com
>
>

Reply via email to