Reverse_delete_rule循环导入

时间:2017-05-08 19:09:31

标签: python mongodb mongoengine

如果我有两个模型,UserAddress,他们会像这样互相引用:

# usermodel.py
from address import Address
from mongoengine import *
class User(Document):
    name = StringField()
    address = ReferenceField(Address)

# address.py
from mongoengine import *
class Address(Document):
     owner = ReferenceField('User', reverse_delete_rule=2)

我收到错误消息:

NotRegistered('`User` has not been registered in the document registry.
Importing the document class automatically registers it, has it been imported?',)

我可以在一个文档上设置信号,在另一个文档上设置reverse_delete_rule吗?我相信这种情况正在发生,因为reverse_delete_rule需要注册文档。

1 个答案:

答案 0 :(得分:0)

这可以通过使用reverse_delete_rule的备用表示法来解决:

User.register_delete_rule(Address, 'owner', CASCADE)

所以文件会像这样更新:

# usermodel.py
from address import Address
from mongoengine import *
class User(Document):
    name = StringField()
    address = ReferenceField(Address)

User.register_delete_rule(Address, 'owner', CASCADE)

# address.py
from mongoengine import *
class Address(Document):
     owner = ReferenceField('User')