How to add a classmethod in Python dynamically

前端 未结 4 1295
無奈伤痛
無奈伤痛 2021-02-14 04:15

I\'m using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances.

class HappyClass(object):
    @classme         


        
4条回答
  •  梦毁少年i
    2021-02-14 04:40

    How I achieved it:

    @classmethod
    def search_url(cls):
        if not hasattr(cls, '_search_url'):
            setattr(cls, '_search_url', reverse('%s-search' % cls._meta.model_name))
        return cls._search_url
    

提交回复
热议问题