On Thursday, March 24, 2011 10:04:47 AM UTC-4, NewBeen wrote:
>
> Hello guys,
>
> I have this class in a model,
>
> Class Order:
>
> def __init__(self):
> self.basket={"product":{},"quantity":{}}
>
> def addProduct(self, Item):
> self.basket["product"]["name"]=item["name"]
>
>
> session.order=Order()
>
> So far so good, but when I execute this, the class is always been
> initialize, so I lose my basket between pages...
> Any ideas how can I fix this, if I put a class in a module and import
> it will it fix it?
I think with that code, every request is just going to create a new Order()
object and write it to session.order (replacing any existing session.order).
If you want to add items to the order and maintain those items in the
session in between page requests, then I would think at each request you
would have to first check to see if there is an existing order stored in the
session, and if so, retrieve it from the session, update it, and then write
the updated order back to the session. Maybe something like:
order = session.order or Order() # Note, if session.order doesn't exist, it
will return None, and a new Order() will be created.
[insert code to update the order]
session.order = order
There is an additional problem, though. According to Massimo, web2py cannot
store instances of classes in the session:
https://groups.google.com/d/msg/web2py/dmN54cpMuXo/lufvxmaQMLUJ. Perhaps
Massimo has more to say regarding this example.
Finally, I think new-style classes are generally recommended, so you would
do 'class Order(object):' instead of 'class Order:'.
Anthony