Google App Engine (Python) 中的电子商务产品类别
E-commerce Product Categories in Google App Engine (Python)
我的目标是创建一个电子商务网站,客户可以在其中的任何产品页面上看到相关产品(类似于amazon.com)。
我不知道如何开始这样一项艰巨的任务。根据我的研究,我的猜测是执行以下操作:
创建一个Category
种类:
class Category(ndb.Model):
name = ndb.StringProperty()
每当创建产品时,通过祖先关系将其与类别相关联:
parent_category = ndb.Key("Category", "Books")
new_product = Product(
title="Coding Horrors Book",
parent=parent_category).put()
现在,在每个产品页面上,我可以创建一个查询 return 图书列表作为 相关产品。
我对这种方法有些顾虑:
首先,这并不是一个可靠的方法。
如何指定产品类别之间的层级关系?例如,如果我们有两个产品类别,"AngularJS"、"VueJS",我们如何指定这两个类别在某种程度上相关?
首先,澄清一下,实体血统对于建立关系不是强制性的(并且它有一些缺点),请参见. and related Ancestor relation in datastore
您需要考虑 Balancing Strong and Eventual Consistency with Google Cloud Datastore。
答案的其余部分假设没有使用实体祖先。
要将一个产品关联到一个类别(或多个类别,如果需要,使用 repeated properties),您可以:
class Product(ndb.Model):
name = ndb.StringProperty()
category = ndb.KeyProperty(kind='Category', repeated=True)
category = ndb.Key("Category", "Books")
new_product = Product(title="Coding Horrors Book",
category=[category]).put()
这种方法有一个可扩展性问题:如果一个产品属于许多类别,更新类别列表会变得越来越慢(整个实体,逐渐增长,每次都需要 re-written)并且,如果属性是索引,它对exploding indexes problem敏感。
这可以通过将 product-category 关系存储为单独的实体来避免:
class ProductCategory(ndb.Model):
product = ndb.KeyProperty(kind='Product')
category = ndb.KeyProperty(kind='Category')
扩展得更好,但在这种情况下,您需要一个 ProductCategory
查询来确定产品相关类别实体的键,然后进行键查找以获取这些类别的详细信息,等等按照这些思路:
category_keys = ProductCategory.query(ProductCategory.product == product_key) \
.fetch(keys_only=True, limit=500)
if category_keys:
categories = ndb.get_multi(category_keys)
logging.info('product %s categories: %s' \
% (product.title, ','.join([c.name for c in categories])))
我的目标是创建一个电子商务网站,客户可以在其中的任何产品页面上看到相关产品(类似于amazon.com)。
我不知道如何开始这样一项艰巨的任务。根据我的研究,我的猜测是执行以下操作:
创建一个
Category
种类:class Category(ndb.Model): name = ndb.StringProperty()
每当创建产品时,通过祖先关系将其与类别相关联:
parent_category = ndb.Key("Category", "Books") new_product = Product( title="Coding Horrors Book", parent=parent_category).put()
现在,在每个产品页面上,我可以创建一个查询 return 图书列表作为 相关产品。
我对这种方法有些顾虑:
首先,这并不是一个可靠的方法。
如何指定产品类别之间的层级关系?例如,如果我们有两个产品类别,"AngularJS"、"VueJS",我们如何指定这两个类别在某种程度上相关?
首先,澄清一下,实体血统对于建立关系不是强制性的(并且它有一些缺点),请参见
您需要考虑 Balancing Strong and Eventual Consistency with Google Cloud Datastore。
答案的其余部分假设没有使用实体祖先。
要将一个产品关联到一个类别(或多个类别,如果需要,使用 repeated properties),您可以:
class Product(ndb.Model):
name = ndb.StringProperty()
category = ndb.KeyProperty(kind='Category', repeated=True)
category = ndb.Key("Category", "Books")
new_product = Product(title="Coding Horrors Book",
category=[category]).put()
这种方法有一个可扩展性问题:如果一个产品属于许多类别,更新类别列表会变得越来越慢(整个实体,逐渐增长,每次都需要 re-written)并且,如果属性是索引,它对exploding indexes problem敏感。
这可以通过将 product-category 关系存储为单独的实体来避免:
class ProductCategory(ndb.Model):
product = ndb.KeyProperty(kind='Product')
category = ndb.KeyProperty(kind='Category')
扩展得更好,但在这种情况下,您需要一个 ProductCategory
查询来确定产品相关类别实体的键,然后进行键查找以获取这些类别的详细信息,等等按照这些思路:
category_keys = ProductCategory.query(ProductCategory.product == product_key) \
.fetch(keys_only=True, limit=500)
if category_keys:
categories = ndb.get_multi(category_keys)
logging.info('product %s categories: %s' \
% (product.title, ','.join([c.name for c in categories])))