code as below:
class A(Document):
name = StringField()
ref_b= ListField(ReferenceField('B', reverse_delete_rule=PULL))
class B(Document):
content = StringField()
ref_a = ReferenceField('A', reverse_delete_rule=NULLIFY)
User Doc has a forward references Page, when i set a reverse_delete_rule other than DO_NOTHING,it will raise
mongoengine.errors.NotRegistered: `B` has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
I want to automatically delete reference when I delete document
how can i avoid this Exception?
I worked around this by separating the delete rule for model that referenced the "unregistered" model. I had to register the delete rule for the model defined first(User in your example). Yours would look like this I think:
class User(Document):
name = StringField()
ref_page= ListField(ReferenceField('Page'))
class Page(Document):
content = StringField()
ref_user = ReferenceField(User, reverse_delete_rule=NULLIFY)
User.register_delete_rule(Page, "ref_page", PULL)
@reasonman Yes,that’s it, I find it in API Reference, Thanks a lot!
Most helpful comment
I worked around this by separating the delete rule for model that referenced the "unregistered" model. I had to register the delete rule for the model defined first(User in your example). Yours would look like this I think: