簡體   English   中英

我如何使用 Django-mptt?

[英]How can i use Django-mptt?

我想通過 Django 中的類別和子類別進行導航。

我安裝了 django-mptt,現在我得到了這個錯誤。"type object 'Category' has no attribute '_mptt_meta'"

模型.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']

預先感謝。

您需要使Category成為MPTTModel的子類:

from mptt.models import MPTTModel, TreeForeignKey

#                ↓ subclass of MPTTModel
class Category(MPTTModel):
    # …

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM