python - 如何在python中继承类(odoo框架 Controller )

标签 python odoo

我在 Python 中遇到了继承问题。我希望程序的输出是:

# 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

但我得到以下输出:

# 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

这是我的代码:

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()

我已经阅读了很多有关继承的内容,但仍然找不到解决此问题的方法。 可能是因为 odoo python 中有不同的系统吗?

编辑: 这是基于 @Bruno 的答案的有效代码。

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()

最佳答案

警告:我根本没有使用过 odoo,所以这个答案部分是根据我从文档中得到的内容和你描述的行为进行的疯狂猜测。

According to the doc ,看起来从 Controller 继承实际上会覆盖原始 Controller ,而不是添加新 Controller (注意上面的警告好吗?)。如果是这样,您可以尝试的一件事是使用多重继承,将特定功能提取到非 Controller 基类:

class Animal(object):
    name = 'Animal'
    ability = 'none'

    def message(self):
        # using string formatting for improved readability
        return "Name: {self.name} | Ability : {self.ability}".format(self=self)


class Bird(Animal):
    name = "Bird"
    ability = Fly

# etc

class AnimalController(Animal, http.Controller):
    @http.route('/animal', auth='public', type='http', website=True, csrf=False)
    def util(self, **kwargs):
        return self.message()

class BirdController(Bird, http.Controller):
    @http.route('/animal/bird', auth='public', type='http', website=True, csrf=False)
    def util(self, **kwargs):
        return self.message()

但这对我来说看起来并不是一个好的设计。 Controller 通常可以处理许多路由(这是所有 MVC Web 框架的情况,并且由于 route 装饰器将应用于方法,我假设这在这里工作相同),因此它可能更简单仅保留一个 Controller 并委托(delegate)给您的 Animal 层次结构:

class Animal(object):
    name = 'Animal'
    ability = 'none'

    def message(self):
        # using string formatting for improved readability
        return "Name: {self.name} | Ability : {self.ability}".format(self=self)


class Bird(Animal):
    name = "Bird"
    ability = Fly

# etc


class Controller(http.controller):
    def __init__(self, ...):
        self._animal = Animal()
        self._bird = Bird()
        # etc

    @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()

   # etc

这当然是不完整的示例代码,因此您可以了解总体思路,并且显然需要进行修改以匹配 odoo 的期望和您的项目的需求。

关于python - 如何在python中继承类(odoo框架 Controller ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51979928/

相关文章:

python - 如何使用 Python 请求获取请求 header 而不是响应 header

odoo - 如何在 odoo 中添加顶级菜单项

python - 使用 python 在远程机器上列出 HDFS 目录

python - MD5 key 加密

odoo-10 - Odoo 10 - QWeb 在报告中包含条形码

python - 我们可以用openERp计算净工资吗

docker - 无法识别Docker Odoo 13.0中的附加组件

python - 使用 on_change 更改选择字段的值

python 到 mysql 尝试异常中的未知列

python - 扭曲的客户端/服务器通信问题