开发者

mongoengine cross object links

开发者 https://www.devze.com 2023-03-05 15:11 出处:网络
I\'ll really new in mongo and mongoengine. I\'ll whant to create object like this: class Candle(Document):

I'll really new in mongo and mongoengine. I'll whant to create object like this:

class Candle(Document):
    value = IntField()
    next = ReferenceField(Candle)
  开发者_JAVA技巧  prev = ReferenceField(Candle)

For using like this:

if Candle.value > Candle.next.value:
    do smf

Is it possible? I'll really glad to see some useful answers.


You need to use the string 'self' as the argument to ReferenceField when you're referring to the class being defined.


Just add the same issue, and I found the solution. I know it's been a while since the question has been asked, but it still may be usefull for some people.

Try:

class Candle(Document):
    value = IntField()
    next = ReferenceField('Candle')
    prev = ReferenceField('Candle')

Using quotes will avoid circular imports and everything should work as wanted.

0

精彩评论

暂无评论...
验证码 换一张
取 消