Mongoengine查询列表字段

时间:2015-04-15 07:30:29

标签: python django mongoengine

我有以下型号:

class Deal(Document):
    stores = ListField(ReferenceField('Store', dbref=False), required=True)

class Store(Document):
    fb_page_id = StringField(verbose_name=_('Facebook PageId'), default="")

我试过通过store.fb_page_id

查询Deal
def get_current_deal_by_fb_page_id(self, request):
    pageid=1556714851256344
    store=Store.objects(fb_page_id=pageid)
    return Deal.objects(stores__fb_page_id=pageid).all()

def get_current_deal_by_fb_page_id(self, request):
    pageid=1556714851256344
    return Deal.objects(__raw__={'stores.fb_page_id': pageid})

但我无法获取数据。这有什么不对?

1 个答案:

答案 0 :(得分:0)

我找到了答案:

class Deal(Document):
    stores = ListField(ReferenceField('Store', dbref=False), required=True)

    @classmethod
    def deals_by_fb_page(cls, fb_page_id):
        deals = []
        for deal in cls.objects():
            for store in deal.stores:
                if store.fb_page_id == fb_page_id:
                    deals.append(deal)
        return deals