I'm obviously super new at this. I appreciate any gentle pointers,
guidelines and best practices on when to use classes when writing code
with web2py (and/or python for that matter).
I ask this because:
- In Ruby on Rails, everything is put under a class of some kind
- I have briefly seen a few discussion posts on here that have used
classes in their code
- I want to learn from experienced python coders (like you!)
Thank you.
http://www.slideshare.net/martinpm/web2py-pensando-en-grande-9448110
I can't read Spanish so I don't understand most of it, but I briefly
caught the idea to use classes in modules (define tables).
I have an opinion about that.
For me the /models folders should be renamed to /scripts since it has scripts that are executed in every request.
I am now recommending to all of my students and clients to avoid the use of models and go to class based system in modules
Model files are run on every request - does that mean having a complex
database structure would slow the entire site down?
Also, isn't it sometimes useful to put code in a model file that you want to run on every (or nearly every) request without having to do an import?
def myajaxcallback():
from datamodel.myobject import MyClass
myobject = MyClass('myparams')
return myobject.counter()
in module:
from gluon import current
from basemodel import BaseModel # a place where I have code which uses class attributes to define the tables
class MyClass(BaseModel):
def __init__(self, params):
from mydb import DataBase # a custom subclass of DAL
self.db = DataBase([theonlytableIneed]) # will return DAL instance with only the table I need defined
def counter(self):
arg = current.request.args(0)
return self.db(self.db.theonlytableIneed.field == arg).select(cache=(cahe.ram, 300))
Model files are run on every request - does that mean having a complex
database structure would slow the entire site down?
Kenji
> > 1. Avoid models,usemodules and import what you need in controller.
I'm interested in this approach and these samples are very helpful but it seems like there is still a ways to go. Would it be possible to create some sort of welcome-like model-less bootstrap?