为什么在Django启动时实例化序列化程序?

时间:2019-02-20 11:22:05

标签: django django-rest-framework

我使用的是Django rest框架,我很困惑在Django启动时实例化序列化程序。

如何停止这种行为?

class MySerializer(ModelSerializer):
    def __init__(self):
        print('hello!') # <= this called when django starts!!

P.S。

原因是序列化程序作为其他序列化程序的类字段属性(作为嵌套字段)被调用。因此,将MySerializer导入为模块时将实例化AnotherSerializer

class AnotherSerializer(ModelSerializer):
    some_nested_field = MySerializer()

所以,我想知道的是,仅当实际请求到达时(而不是在模块加载时)如何实例化MySerializer

0 个答案:

没有答案