I've encountered an inheritance problem in Python. I'd expect the output from by program to be:
# url: home/animal
response: CLASS: Animal | Ability : none
# url: home/animal/bird
response: CLASS: Bird | Ability : Fly
# url: home/animal/fish
response: CLASS: Fish | Ability : Swim
But I get the following output:
# url: home/animal
response: CLASS: Fish | Ability : Swim
# url: home/animal/bird
response: CLASS: Fish | Ability : Swim
# url: home/animal/fish
response: CLASS: Fish | Ability : Swim
Here is my code:
class Animal(http.Controller):
name = 'Animal'
ability = 'none'
@http.route('/animal', auth='public', type='http', website=True, csrf=False)
def util(self, **kwargs):
return self.message()
def message(self):
return "Name: "+self.name +" | Ability : " + self.ability
class Bird(Animal):
name = 'Bird'
ability = 'fly'
@http.route('/animal/bird', auth='public', type='http', website=True, csrf=False)
def util1(self, **kwargs):
return self.message()
class Fish(Animal):
name = 'Fish'
ability = 'swim'
@http.route('/animal/fish', auth='public', type='http', website=True, csrf=False)
def util2(self, **kwargs):
return self.message()
I've read quite a lot about inheritance, but still couldn't find a solution for this problem. Could it be because it has a different system in odoo python?
Edit: Here is the code that works, based on @Bruno's answer.
class Animal():
name = 'Animal'
ability = 'none'
def message(self):
return "Name: {self.name} | Ability : {self.ability} ".format(self=self)
class Bird(Animal):
name = 'Bird'
ability = 'fly'
class Fish(Animal):
name = 'Fish'
ability = 'swim'
class MyController(http.Controller):
def __init__(self):
self._animal = Animal()
self._bird = Bird()
self._fish = Fish()
@http.route('/animal', auth='public', type='http', website=True, csrf=False)
def animal(self, **kwargs):
return self._animal.message()
@http.route('/animal/bird', auth='public', type='http', website=True, csrf=False)
def bird(self, **kwargs):
return self._bird.message()
@http.route('/animal/fish', auth='public', type='http', website=True, csrf=False)
def fish(self, **kwargs):
return self._fish.message()