如何解决“ TypeError缺少1个必需的位置参数:'on_delete'”

时间:2019-07-29 16:34:49

标签: python django sqlite

我正在为我的博客设置类别标签,但遇到TypeError: init ()缺少1个必需的位置参数:'on_delete',即使我包括了“ on_delete = models.CASCADE ”。

from django.db import models
from django.urls import reverse
from tinymce import HTMLField


class CategoryTag(models.Model):
    title = models.CharField(max_length=150)
    slug = models.SlugField()
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        blank=True,
        null=True,
        related_name='children'
    )

    class Meta:
        unique_together = ('slug', 'parent',)
        verbose_name_plural = "categories"

    def __str__(self): 
        full_path = [self.title]

        k = self.parent

        while k is not None:
            full_path.append(k.title)
            k = k.parent

        return ' -> '.join(full_path[::-1])

class Post(models.Model):
    title = models.CharField(max_length=100)
    overview = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    content = HTMLField()
    comment_count = models.IntegerField(default=0)
    post_img = models.ImageField()
    category = models.ForeignKey('CategoryTag', null=True, blank=True)
    featured = models.BooleanField()
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.title

    def get_cat_list(self):          
        k = self.category
        breadcrumb = ["dummy"]
        while k is not None:
            breadcrumb.append(k.slug)
            k = k.parent

        for i in range(len(breadcrumb)-1):
            breadcrumb[i] = '/'.join(breadcrumb[-1:i-1:-1])
        return breadcrumb[-1:0:-1]

    def get_absolute_url(self): 
        return reverse('post-detail', kwargs={
            'id': self.id
        })

我尝试通过使用删除其他字段(blank = True,Null = True,related_name ='children')来绕过,但我仍然收到相同的错误。

0 个答案:

没有答案