Search code examples
pythongoogle-app-engineweb-applicationsmultiple-inheritancetipfy

Proper way to edit existing entity in tipfy


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?


Solution

  • 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;-).