Skip to main content
 首页 » 编程设计

python之递归问题

2025年05月04日11kerrycode

当我从一个类导入另一个类时遇到问题。我有 不同模块中的那些类:

crm.py

from CRMContactInformation import CRMContactInformation 
 
class CRM(rdb.Model): 
        """Set up crm table in the database""" 
        rdb.metadata(metadata) 
        rdb.tablename("crms") 
 
        id = Column("id", Integer, ForeignKey("screens.id"), 
primary_key=True) 
        screen_id = Column("screen_id", Integer, ) 
 
        contactInformation = relationship(CRMContactInformation, 
userlist=False, backref="crms") 
        .... 

CRMContactInformation.py

from CRM import CRM 
 
class CRMContactInformation(rdb.Model): 
        """Set up crm contact information table in the database""" 
        rdb.metadata(metadata) 
        rdb.tablename("crm_contact_informations") 
 
        id = Column("id", Integer, ForeignKey(CRM.id), primary_key=True) 
        owner = Column("owner", String(50)) 
        ..... 

如你所见,我有一个递归问题,因为我导入 CRM中的CRMContactInformation和CRM中的CRMContactInformation。我有 此错误或类似错误:

“AttributeError:‘module’对象没有属性”

我试图更改导入整个路径的导入。没用 要么出去。

有什么方法可以使用元数据对象来访问 表的属性?还是另一种方法来解决这个问题?

提前致谢!

请您参考如下方法:

延迟导入:

class CRM(rdb.Model): 
        """Set up crm table in the database""" 
        rdb.metadata(metadata) 
        rdb.tablename("crms") 
 
        id = Column("id", Integer, ForeignKey("screens.id"), primary_key=True) 
        screen_id = Column("screen_id", Integer, ) 
 
        .... 
 
from CRMContactInformation import CRMContactInformation 
CRM.contactInformation = relationship(CRMContactInformation, userlist=False, backref="crms")