如何在mongoengine中获取外键关系

How to fetch foreign key relationships in mongoengine

我有以下两个型号。

class URL(mongoengine.Document):
    codes = mongoengine.ListField(mongoengine.IntField(required=True),
               verbose_name='Codes')
    name = mongoengine.StringField(max_length=255, required=True)

class BytesForURL(mongoengine.Document):
    url = mongoengine.ReferenceField(URL)
    value = mongoengine.FloatField(required=True)
    timestamp = mongoengine.DateTimeField(required=True)

我需要在一个时间范围内获取给定 url 名称的 BytesForURL 的数据。我该如何查询。

bytes = mongo_models.BytesForURL.objects(timestamp__gte=from_time,
                                                  timestamp__lte=to_time, )

for byte in bytes:
    print byte.value, byte.timestamp

但是我如何通过 url 获取此值。有帮助吗?

你可以用这个

urls = mongo_models.URL.objects.filter(name=name)
bytes = mongo_models.BytesForURL.objects.filter(
            timestamp__gte=from_time,
            timestamp__lte=to_time, url__in=urls)