【问题标题】:E-commerce Product Categories in Google App Engine (Python)Google App Engine (Python) 中的电子商务产品类别
【发布时间】:2018-08-07 12:08:18
【问题描述】:

我的目标是创建一个电子商务网站,让客户可以在任何产品页面上看到相关产品(类似于 amazon.com)。

我不知道如何开始这项艰巨的任务。根据我的研究,我的猜测是执行以下操作:

  1. 创建一个Category 种类:

    class Category(ndb.Model): 
        name = ndb.StringProperty()
    
  2. 每当创建产品时,通过祖先关系将其与类别相关联:

    parent_category = ndb.Key("Category", "Books")
    new_product = Product(
        title="Coding Horrors Book", 
        parent=parent_category).put()
    
  3. 现在,我可以在每个产品页面上创建一个查询,以将书籍列表作为相关产品返回。

我对这种方法有些担心:

  1. 首先,这感觉不是一个可靠的方法。

  2. 如何指定产品类别之间的层级关系?例如,如果我们有两个产品类别,“AngularJS”、“VueJS”,我们如何指定这两个类别有某种关联?

【问题讨论】:

    标签: python google-app-engine app-engine-ndb


    【解决方案1】:

    首先,澄清一下,实体祖先不是建立关系的强制要求(并且它有一些缺点),请参阅Can you help me understand the nbd Key Class Documentation or rather ancestor relationship?。及相关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() 
    

    这种方法有一个可扩展性问题:如果一个产品属于许多类别,更新类别列表会变得越来越慢(整个实体,逐渐增长,每次都需要重新编写),如果属性被索引,它对exploding indexes problem 很敏感。

    这可以通过将产品-类别关系存储为单独的实体来避免:

    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])))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多