Web2PY and non-persistent models

7 views
Skip to first unread message

luckyboy

unread,
Feb 19, 2009, 3:50:24 PM2/19/09
to web2py Web Framework
Does web2py support non-persistent model classes ? ie, a core model
class where some business logic can be performed while that class
doesn't necessarily (directly) map to a database table ?
For example the model data can be retrieved from a remote web service,
or a set of remote queries ?
In all other MVC frameworks this is possible: In EJB3.0, unless you
declare model as @entity it is not persistent. In RAILS, you can
simply not inherit from ActiveRecord, and you may need to implement
some methods like find, etc.. , or you can use a plugin like
ActiveModel. Is this doable in web2py? My first guess is no.

mdipierro

unread,
Feb 19, 2009, 4:57:27 PM2/19/09
to web2py Web Framework
Yes and no.

Not sure how EJB3 works so I cannot answer yes and no. I can tell you
what you can do.

1) you can define classes

class MyObj:
def say_three(self): return 3

2) you can make instances of the class persistent in various ways by
storing in cache

myobj=cache.ram('myobj',lambda: MyObj(),timeout)

3) you can make instanced persistant for each session

if not session.myobj: session.myobj=MyObj()

4) You can make in memory databases

db=SQBD('sqlite:memory:')
db.define_table('table_in_ram',SQLField('fieldname'))

and you can import/export the data.

import cStringIO
file=cStringIO.String()
db.table_in_ram.export_to_csv_file(file)
file.seek(0)
db.table_in_ram.truncate()
db.table_in_ram.import_form_csv_file(file)

Not sure how this compares with what you have in mind.

I do not expect and I would not want objects that are not stored in
database to expose the same API as if they were.

If you can give me a more explicit example perhaps I can give a
better answer.


Massimo

luckyboy

unread,
Feb 19, 2009, 6:52:44 PM2/19/09
to web2py Web Framework
Thanks, your answer was good enough.
About the statement "I do not expect and I would not want objects that
are not stored in
database to expose the same API as if they were.", well, sometimes you
might want to do that. For example, what if some of your models are
just proxies for their mirror remote persistent classes that are
exposed via a set of web services. Or your data store may be a remote
store (like Amazon's S3) and needn't to be a relational database at
all. Of course you then need to override ORM implementation of these
APIs but still you should be able to this in a way that is seamless to
your controller.

mdipierro

unread,
Feb 19, 2009, 6:55:52 PM2/19/09
to web2py Web Framework
I agree with you.
Reply all
Reply to author
Forward
0 new messages