views:

81

answers:

1

I'm using a PersonEditHandler class in tipfy to edit a Person entity. I have the get() and post() methods formed, but when I reference self.person (to check if the get method found the existing person by key), I get an 'object has no attribute' error.

This is because I never initialize it in the init method since I'm inheriting from RequestHandler and Jinja2Mixin. However, when I override init, I get another error: 'TypeError: init() takes exactly 1 argument (3 given)'

Here is the code:

class PersonEditHandler(RequestHandler, Jinja2Mixin): 
    def __init__(self): 
        PersonEditHandler.__init__(self) 
        # ...or 'super(PersonEditHandler, self).__init__()' 
        self.person = None 

Am I having trouble because of multiple inheritance? What is the best way to edit a retrieved record in tipfy without creating a new one?

+1  A: 

I would recommend foregoing the __init__ and rather adding a class attribute:

class PersonEditHandler(RequestHandler, Jinja2Mixin): 
    person = None

This way, when you access a self.person that's never been set on a specific instance self, it will defer to the class and you'll get None as desired; when you set self.person, it will set it on the entity, as desired.

Multiple inheritance with mixins is OK, in general, but it can make for somewhat murky problems with __new__ and __init__, as you've noticed (honestly I have no idea what class is whining about receiving three arguments here... though it would help if you showed the full traceback, finessing the problem as I just suggested is simpler;-).

Alex Martelli
Thanks, Alex. I've actually already tried this. The problem is that it doesn't look like tipfy's Handlers persist items between get() and post() methods. For example, if I get an entity and store it as self.person in get, I cannot refer to self.person in post(); instead, I have to get the entity again. Someone remarked that I should use memcache to accomplish this. How would I do that?
Wraith
@Wraith, it's not about tipfy -- each GAE request instantiates a brand new handler; but then setting `.person` to `None` in `__init__` would be just as useless (I see it now because you mention "persisting between get and post" -- it's not about the _methods_, if you called one from the other you'd still find everything intact!, it's about being a new **instance**!). You could store the key/person pair in memcache (in GET) and check if it's there (in POST) as long as you know the key in POST. But this Q is focusing on showing entirely the wrong (irrelevant) code, pls ask another!-)
Alex Martelli
Thanks for the great answer again, Alex. I've posted a new question here http://stackoverflow.com/questions/3392990/how-do-i-store-a-fetched-entity-in-memcache-for-app-engine
Wraith