MrK*_*tts 5 python django python-3.x
deconstructiblePython 中的装饰器有什么作用?我遇到了这个装饰器,正在查看其他人的代码,但是我搜索了Python文档,却找不到该装饰器的实际功能?
@deconstructible
class UserValidator(object):
def __call__(self, value):
if value:
etc
Run Code Online (Sandbox Code Playgroud)
给定上面的代码,对此类添加'deconstructible'有什么作用?
def deconstructible(*args, **kwargs):
"""
Class decorator that allow the decorated class to be serialized
by the migrations subsystem.
Accepts an optional kwarg `path` to specify the import path.
"""
Run Code Online (Sandbox Code Playgroud)
通过使用Google,我设法发现它是Django的一部分:
https://docs.djangoproject.com/en/1.10/topics/migrations/#adding-a-deconstruct-method
只要类的构造函数的所有参数本身都是可序列化的,就可以使用django.utils.deconstruct中的@deconstructible类装饰器来添加deconstruct()方法。
| 归档时间: |
|
| 查看次数: |
3975 次 |
| 最近记录: |