【问题标题】:How can i use Django-mptt?我如何使用 Django-mptt?
【发布时间】:2020-12-06 13:44:44
【问题描述】:

我想在 Django 中的类别和子类别中进行导航。

我安装了 django-mptt,现在我得到了这个错误。“类型对象 'Category' 没有属性 '_mptt_meta'”

models.py

from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
class Category(models.Model):
    name=models.CharField(max_length=50)
    slug=models.SlugField(max_length=50, unique=True, help_text='Uniue Value product page url, created from name.')
    is_active=models.BooleanField(default=True)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now=True)
    parent=TreeForeignKey('self',on_delete=models.CASCADE,null=True,blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by=['name']

先谢谢了。

【问题讨论】:

    标签: python python-3.x django django-models django-views


    【解决方案1】:

    您需要将Category 设为MPTTModel 的子类:

    from mptt.models import MPTTModel, TreeForeignKey
    
    #                ↓ subclass of MPTTModel
    class Category(MPTTModel):
        # …

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多