如何在 Django 模型中构建嵌套类别

How to structure nested categories in django models

我正在开发一个在线购物车项目,在 product-catalog-app 中我有点迷茫如何构建类别,例如,以下序列:MEN -> 鞋类-> 运动鞋 -> 某些品牌 (NIKE)-> 实际产品。正如你所看到的深度是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,...)
   ...

这只是一个建议,您可以想出更有效的方法,例如,

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

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