gpt4 book ai didi

django - 类型错误 : __init__() got an unexpected keyword argument 'on_delete'

转载 作者:行者123 更新时间:2023-12-03 18:40:55 25 4
gpt4 key购买 nike

我建立了一个模型:

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,)

当我做 makemigration 它说:
TypeError: __init__() got an unexpected keyword argument 'on_delete'

当删除 on_delete 它说:
TypeError: __init__() missing 1 required positional argument: 'on_delete'

穿什么?

最佳答案

on_delete 不是 ManyToManyField 上的有效参数。您需要在 on_deleteForeignKey 模型中的每个 through 字段中使用 ChannelMembers 参数。
您可以通过以下示例了解如何在 Django Docs website 上实现
在您的情况下,它看起来像这样:

class Channel(models.Model):
...
# Don't have the on_delete parameter on this field
subscribers = models.ManyToManyField(User, through="ChannelMembers")

# Add on_delete to both the fields in this class instead
class ChannelMembers(models.Model):
channel = models.ForeignKey(Channel, on_delete=models.CASCADE)
subscriber = models.ForeignKey(User, on_delete=models.CASCADE)

关于django - 类型错误 : __init__() got an unexpected keyword argument 'on_delete' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com