Pyramid view class inheritance with @view_defaults and @view_config decorators

痴心易碎 提交于 2019-12-04 08:26:03

@view_config is a venusian decorator, and not a strictly traditional decorator. Not until .scan() is called will anything take effect.

This also means that they are not inherited, however venusian provides a class decorator named lift() that will do exactly as you wish.

The venusian API documentation shows that something like the following should work for your use case:

from venusian import lift

@view_defaults(route_name='view_a', renderer='templates/views.mak')
class View_A(object):

    def message(self):
        return 'This is view a'

    @view_config(request_method='GET')
    def get(self):
        return {'message': self.message()}

@view_defaults(route_name='view_b')
@lift()
class View_B(View_A):

    def message(self):
        return 'This is view b'

At this point all your inherited functions will correctly have the @view_config applied. Now upon running .scan() your application will behave as expected.


Do note, that the inheritance of @view_defaults may change in the future: https://github.com/Pylons/pyramid/issues/1382.

This may or may not change your views as listed, depending on whether you expect the renderer to carry over from the super class.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!