TypeError:__init __()获得了意外的关键字参数'on_delete'

时间:2018-11-04 14:05:30

标签: django django-models

我建立了一个模型:

transition

当我进行移民时,说:

$this->db->select('posts.*, categories.*');
$this->db->from('posts');
$this->db->join('categories','posts.cat_id= categories.id','INNER');
$this->db->where('posts.id',2);
$res = $this->db->get();

当删除 on_delete 时,它会说:

class Channel(models.Model):
    title = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(allow_unicode=True, unique=True)
    description = models.TextField(blank=True, default='')
    description_html = models.TextField(editable=False, default='', 
blank=True)
    subscribers = models.ManyToManyField(User, 
through="ChannelMembers", on_delete=models.CASCADE,)

穿什么?

1 个答案:

答案 0 :(得分:1)

on_deleteManyToManyField上不是有效的参数。您需要在on_delete模型的ForeignKey模型的每个through字段中使用ChannelMembers自变量。

您可以看到以下示例,如何在Django Docs website

上实现此目标

在您的情况下,它看起来像这样:

class ChannelMembers(models.Model):
    channel = models.ForeignKey(Channel, on_delete=models.CASCADE)
    subscriber = models.ForeignKey(User, on_delete=models.CASCADE)