最近,也许在软件升级之后,我遇到了一个问题:distinct()过滤现在返回一个字典集合而不是EmbeddedDocument集合。意味着对象的直接遍历按预期工作。
这是一段代码,可能不会被删除而是相当简单。这是一个简单的产品目录,其功能包括按功能过滤等功能:
from mongoengine import *
class FeatureInfo(Document):
title = StringField()
class ProductFeature(EmbeddedDocument):
featureInfo = ReferenceField(FeatureInfo)
value = StringField()
def eq(a, b):
return a.featureInfo.title == b.featureInfo.title and a.value == b.value
class Product(Document):
title = StringField()
features = ListField(EmbeddedDocumentField(ProductFeature))
def populate():
producer_feature_info = FeatureInfo("Producer")
producer_feature_info.save()
screen_size_feature_info = FeatureInfo("Screen size")
screen_size_feature_info.save()
iphone_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Apple")
iphone_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="4 inch")
iphone = Product(title="iPhone 5S")
iphone.features.append(iphone_producer_feature)
iphone.features.append(iphone_screen_size_feature)
iphone.save()
galaxy_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Samsung")
galaxy_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="5 inch")
galaxy = Product(title="Galaxy S4")
galaxy.features.append(galaxy_producer_feature)
galaxy.features.append(galaxy_screen_size_feature)
galaxy.save()
galaxy_producer_feature = ProductFeature(featureInfo=producer_feature_info, value="Samsung")
galaxy_screen_size_feature = ProductFeature(featureInfo=screen_size_feature_info, value="4 inch")
galaxy = Product(title="Galaxy S2")
galaxy.features.append(galaxy_producer_feature)
galaxy.features.append(galaxy_screen_size_feature)
galaxy.save()
if __name__ == "__main__":
connect('test')
populate()
products = Product.objects()
print "\n".join(map(lambda p: p.title + " [" + ", ".join(map(lambda f: f.featureInfo.title + " = " + f.value, p.features)) + "]", products))
print "\nProduct features"
features = []
for product in products:
print product.title
for feature in product.features:
if feature not in features:
features.append(feature)
print feature.__class__.__name__
print "\nUnique features"
for feature in features:
print feature.featureInfo.title + " = " + feature.value + " (" + feature.__class__.__name__ + ")"
print "\nDistinct features"
features = products.distinct("features")
for feature in features:
print feature.__class__.__name__
这是输出:
python test.py
iPhone 5S [Producer = Apple, Screen size = 4 inch]
Galaxy S4 [Producer = Samsung, Screen size = 5 inch]
Galaxy S2 [Producer = Samsung, Screen size = 4 inch]
Product features
iPhone 5S
ProductFeature
ProductFeature
Galaxy S4
ProductFeature
ProductFeature
Galaxy S2
ProductFeature
ProductFeature
Unique features
Producer = Apple (ProductFeature)
Screen size = 4 inch (ProductFeature)
Producer = Samsung (ProductFeature)
Screen size = 5 inch (ProductFeature)
Distinct features
BaseDict
BaseDict
BaseDict
BaseDict
我在StackOverflow上发现了这样一个问题,但MongoEngine 0.8.3存在问题,0.8.4存在问题。我跟着它升级但没有效果。