【问题标题】:How to structure nested categories in django models如何在 django 模型中构建嵌套类别
【发布时间】:2018-01-14 12:40:26
【问题描述】:

我正在开发一个在线购物车项目,在 product-catalog-app 中我有点偶然发现如何构建类别,例如,以下顺序:MEN-> FOOTWEAR-> SPORTS鞋子 -> 一些品牌(耐克)-> 实际产品。如您所见,深度为5。在每个级别内制作sub sub sub...类别是否是一个好的设计

class Category:
   pass

class SubCategory:
   category=models.ForeignKey(Category,...)
   ...

class SubSubCategory:
   category=models.ForeignKey(SubCategory,...)
   ...

class BrandOrSmthEle:
   category=models.ForeignKey(SubSubCategory,...)
   ...

class Product:
   category=models.ForeignKey(BrandOrSmthEle,...)
   ...

【问题讨论】:

标签: python django database orm data-modeling


【解决方案1】:

这只是一个建议,您可以考虑更有效的方式,例如,

class Gender(Model):
    #male/female
    .....

class Genre(Model):
    #casual/sports/party
    ......

class Type(Model):
    #footwear/clothes/hats
    ......

class Brand(Model):
    #Nike/Adidas/Puma
    .....

class Product(Model):
    gender = ForeignKey(Gender)
    genre = ForeignKey(Genre)
    type = ForeignKey(Type)
    brand = ForeignKey(Brand)
    ......

您可以通过使用这种层次结构来避免嵌套架构。

【讨论】:

  • 哇,很好的建议,我怎么没想到这个:),真的很简单
猜你喜欢
  • 2019-10-25
  • 1970-01-01
  • 2020-07-10
  • 2017-01-29
  • 1970-01-01
  • 2023-01-12
  • 2021-04-26
  • 2011-03-26
  • 1970-01-01
相关资源
最近更新 更多