简体   繁体   English

如何将嵌套类别添加到Django模型?

[英]How do I add nested categories to a Django model?

What I am trying to achieve is to design a model which has nested levels of categories. 我想要实现的是设计一个具有嵌套级别的类别的模型。

the levels are like this. 水平是这样的。

category0 > category1 > category2 > category3 > category4 > category5

Posts can have levels from 0 - 5 , so a post can have category 0 - 1 , while other post may have 0 - 4 or 0 - 5 , 帖子的级别可以是0 - 5 ,所以帖子可以有0 - 1类,而其他帖子可以有0 - 40 - 5

The category on the given highest level ( 0 is the lower while 5 is the highest) should inherit from the one just below it, ( 1 > 2 > 3 > 4 > 5 ) 给定最高级别的类别( 0是较低而5是最高级别)应该从它下面的那个继承,( 1 > 2 > 3 > 4 > 5

How can I achive this? 我怎么能得到这个?

My current categories looks like this 我目前的类别看起来像这样

class Category0(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)


class Category1(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)
    parent = models.ForeignKey(Category0)


class Category2(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)
    parent = models.ForeignKey(Category1)


class Category3(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)
    parent = models.ForeignKey(Category2)


class Category4(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)
    parent = models.ForeignKey(Category3)


class Category5(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)
    parent = models.ForeignKey(Category4)

And the post model here 而帖子模型在这里

class Product(models.Model):
    title = models.CharField(max_length=20)
    slug = AutoSlugField(unique=True, populate_from='title')
    content = models.TextField(blank=True)
    category = models.ForeignKey(CategoryChild4)

What would be the best method? 什么是最好的方法? any suggestions or changes welcome. 任何建议或变更欢迎。

Update 更新

The backend is PostgrSQL. 后端是PostgrSQL。

Thanks 谢谢

Could you use a Foreign Key to self approach? 你能用Foreign Key self接近吗?

For example: 例如:

class Category(models.Model):

    parent = models.ForeignKey('self', default=None, null=True, blank=True, related_name='nested_category')
    nesting_level = models.IntegerField()
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=60)

It's probably useful to keep track of the nesting levels explicitly with nesting_level but this way you can create the relationship you describe. 使用nesting_level显式跟踪嵌套级别可能很有用,但这样您就可以创建所描述的关系。

If you are using Postgres as your database, I probably would not take this approach. 如果您使用Postgres作为数据库,我可能不会采用这种方法。 I would look into using the new json datatype (postgres 9.3>). 我将研究使用新的json数据类型(postgres 9.3>)。 If you want to utilize this you can store this type of information in document form (think mongo-db but in an RDBMS). 如果你想利用它,你可以以文档形式存储这种类型的信息(想想mongo-db,但在RDBMS中)。 I recommend installing this field: 我建议安装此字段:

https://github.com/bradjasper/django-jsonfield/ https://github.com/bradjasper/django-jsonfield/

The only downside is that you need to perform validation yourself, but since this a multiple layered model, you would probably need to do that anyway based on the way you are doing it above. 唯一的缺点是你需要自己进行验证,但由于这是一个多层模型,你可能需要根据你上面的方式做到这一点。

You could look into using MongoDB also.... 您也可以考虑使用MongoDB ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM