如何覆盖使用过的装饰器?

时间:2015-01-30 15:54:48

标签: python django python-2.7 decorator python-decorators

我在django-fsm中使用了django,我想在Model中覆盖/重用转换装饰器for field。例如,其状态字段包含我的模型的继承TransitionMixin类:

a)django中的基础模型

class BlogPage(TransitionMixin(myapp, mymodelname, state_obj, state), models.Model):
      page_id = IntegerField(...)
      state = StateObject(...) # its tuple with values similar on ('d', draft,)/etc.   

b)mixin for class

from django_fsm import transition

class TransitionMixin(object):
    def __init__(self, appname, modelname, field, state_obj):
        self.appname = appname
        self.modelname = modelname
        self.State = state_obj
        self.state = field

    # how to define there dynamically decorator with users parameters?
    @transition(field=state, source=State.READY_FOR_REVIEW,
                target=State.PUBLISHED,
                permission='{0}.can_publish_{1}'.format(self.appname,
                                                        self.modelname))
    def publish(self):
        pass

    # some other methods for state field, with @transition decorator
    # ...

我怎么能这样做?也许使用元类或者可能存在更灵活的解决方案?

0 个答案:

没有答案