Web2Py with traditional Python classes

349 views
Skip to first unread message

Deckarep

unread,
Aug 2, 2010, 12:32:44 PM8/2/10
to web2py-users
Hello Web2Py group,

I'm am currently evaluating different Python-based Web Application
frameworks and had a question regarding Web2Py and the use of
traditional Python classes.

My question is that why do you not create true Python classes (using
the class) keyword when developing the models? For example, our
Models that we use today are very large and complex. We have many
instance methods defined with our models but today our models are true
Python classes. We have database readers and writers which are
responsible for pulling the data from our DB which then create and
populate our models.

But in Web2Py it doesn't seam like we have the ability. Suppose I
have a Contact model...how would I build it in web2py such that I can
have various instance methods/attributes associated with that Contact
model? It seams to me that with web2py we just have to work with raw
data fields that come back from DAL queries.

So what is the recommended way to accomplish this if possible?

Thanks in advance and I appreciate the help.

-Ralph

mdipierro

unread,
Aug 2, 2010, 2:25:47 PM8/2/10
to web2py-users
I think your question is why do we provide a Database Abstraction
Layer and not an Object Relational Mapper.

The answer is because the difference is semantic and not functional.
In my view the mapping between tables in relational database and
objects is a too much of a stretching that works well in simple cases
but not in very complex cases. I think the DAL approach is more
transparent since maps more intuitively into the SQL constructs.
Moreover a DAL tends to be faster than an ORM because there is less
metaclass mambo-jumbo.

Yet you can do what you asked using computed fields and virtual fields
as explained here:

http://www.web2py.com/book/default/chapter/06#Computed-fields

Let me give you an example of of why I think our approach is better.

Say you have a table order (with a field quantity and a reference to a
product) and a table product (with a field unit_price). If you join
the table and want a new virtual field total_price ( = quanty*
unit_price) web2py allows you to do so. The virtual fields can be
attached to a table but also to a join while ORM usually do not allow
you do so because results of a joins are not classes.

Massimo
Reply all
Reply to author
Forward
0 new messages