如何解决这个特定的python循环导入?

如何解决这个特定的python循环导入?,python,circular-dependency,Python,Circular Dependency,有人能帮我完成这个python循环导入吗 文件measurement\u schema.py导入elementary\u process\u schema。文件elementary\u process\u schema.py导入measurement\u schema 我需要在每个声明类的最后一行中使用引用的类。e、 g.measurement\u schema.py的最后一行:elementary\u processs=fields.Nested(elementaryprocesschema,

有人能帮我完成这个python循环导入吗

文件
measurement\u schema.py
导入
elementary\u process\u schema
。文件
elementary\u process\u schema.py
导入
measurement\u schema

我需要在每个声明类的最后一行中使用引用的类。e、 g.measurement\u schema.py的最后一行:
elementary\u processs=fields.Nested(elementaryprocesschema,many=True)

完整代码:

measurement\u schema.py

from marshmallow import fields

from api import ma
from api.model.schema.elementary_process_schema import ElementaryProcessSchema


class MeasurementSchema(ma.Schema):


    id = fields.Int(dump_only=True)
    name = fields.Str()
    description = fields.Str()
    created_on = fields.Str()

    elementary_processes = fields.Nested(ElementaryProcessSchema, many=True)
from marshmallow import fields

from api import ma
from api.model.schema.ar_rl_schema import ARRLSchema
from api.model.schema.data_item_schema import DataItemSchema
from api.model.schema.elementary_process_type_schema import ElementaryProcessTypeSchema
from api.model.schema.measurement_schema import MeasurementSchema


class ElementaryProcessSchema(ma.Schema):
    id = fields.Int(dump_only=True)
    name = fields.Str()
    operation = fields.Str()
    reference = fields.Str()
    created_on = fields.Str()

    elementary_process_type = fields.Nested(ElementaryProcessTypeSchema)
    data_itens = fields.Nested(DataItemSchema, many=True)
    AR_RLs = fields.Nested(ARRLSchema, many=True)

    measurement = fields.Nested(MeasurementSchema)
基本过程模式.py

from marshmallow import fields

from api import ma
from api.model.schema.elementary_process_schema import ElementaryProcessSchema


class MeasurementSchema(ma.Schema):


    id = fields.Int(dump_only=True)
    name = fields.Str()
    description = fields.Str()
    created_on = fields.Str()

    elementary_processes = fields.Nested(ElementaryProcessSchema, many=True)
from marshmallow import fields

from api import ma
from api.model.schema.ar_rl_schema import ARRLSchema
from api.model.schema.data_item_schema import DataItemSchema
from api.model.schema.elementary_process_type_schema import ElementaryProcessTypeSchema
from api.model.schema.measurement_schema import MeasurementSchema


class ElementaryProcessSchema(ma.Schema):
    id = fields.Int(dump_only=True)
    name = fields.Str()
    operation = fields.Str()
    reference = fields.Str()
    created_on = fields.Str()

    elementary_process_type = fields.Nested(ElementaryProcessTypeSchema)
    data_itens = fields.Nested(DataItemSchema, many=True)
    AR_RLs = fields.Nested(ARRLSchema, many=True)

    measurement = fields.Nested(MeasurementSchema)

我知道关于这个问题有很多话题。但是,我无法解决我的特定循环引用问题。

这是ORMs和python的一个常见问题,通常采用相同的方法解决:通过名称(字符串)而不是引用(类/实例)标识关系。在棉花糖文档中有很好的记录:

简言之,试试这样的方法(我对棉花糖没有任何经验,所以这是没有经过测试的):


您是否在这里探索过实现工厂模式,下面是一个示例-在您的类中,您可能需要在MeasurementSchema中注入ElementaryProcessSchema,在工厂中注入ElementaryProcessSchema,反之亦然。谢谢Hussain。我用了斯马西的小费。它很简单,工作也很好。此外,我应该包括“exclude=('bounders',)”如下所述:measurement=fields.Nested(“MeasurementSchema”,exclude=('elementary_processs',))