3

Given the following Code:

from mongoengine import *

connect('spike_bidirectional')

class User(Document):
    name = StringField()
    page = ListField(ReferenceField('Page'))

class Page(Document):
    content = StringField()
    user = ReferenceField(User)

u = User(name="Test User").save()
p = Page(content="Page 1", user=u).save()
p2 = Page(content="Page 2", user=u).save()

u.reload()
p.reload()
x = u.pages

x is always empty. The Database itself looks pretty well. Is there a way to access all Pages related to a userobject directly from the user?

If Bidirectional connections are possible i wonder what the rules are to define the owning and the referenced site in the datamodel. Are the names of the attribute itself relevant?

Thomas Spycher
  • 956
  • 13
  • 32

1 Answers1

0

Which version of Mongoengine are you using? I believe 0.8 and above do not dereference unless you tell it to explicitly.

Create a test database and try with the following changes to your model.

page = ListField(ReferenceField(Page, dbref=True))

user = ReferenceField(User, dbref=True)

I've not tested, first I would check and be sure the version of Mongoengine being used.

Dillon
  • 106
  • 5