python - 类型错误 : __init__() missing 1 required positional argument: 'on_delete' (Django 2)

标签 python django python-3.x django-filer

我需要将所需的参数:on_delete = models.CASCADE 添加到以下代码中。否则,我会得到以下内容

TypeError: __init__() missing 1 required positional argument: 'on_delete'.

我应该如何以及在哪里执行此操作?

class FilerFileField(models.ForeignKey):
    default_form_class = AdminFileFormField
    default_model_class = File 

    def __init__(self, **kwargs):
        # We hard-code the `to` argument for ForeignKey.__init__
        dfl = get_model_label(self.default_model_class)
        if "to" in kwargs.keys():  # pragma: no cover
            old_to = get_model_label(kwargs.pop("to"))
            if old_to != dfl:
                msg = "%s can only be a ForeignKey to %s; %s passed" % (
                    self.__class__.__name__, dfl, old_to
                )
                warnings.warn(msg, SyntaxWarning)
        kwargs['to'] = dfl
        super(FilerFileField, self).__init__(**kwargs)

    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': self.default_form_class,
        }
        try:
            defaults['rel'] = self.remote_field
        except AttributeError:
            defaults['rel'] = self.rel
        defaults.update(kwargs)
        return super(FilerFileField, self).formfield(**defaults)

最佳答案

它将位于 __init__ 函数的最后一行 super(FilerFileField, self).__init__(**kwargs) 中。

您可以看到on_deleteForeignKey类的必需参数。 https://docs.djangoproject.com/en/2.2/ref/models/fields/#foreignkey https://docs.djangoproject.com/en/2.2/ref/models/fields/#arguments

类似于 super(FilerFileField, self).__init__(on_delete=models.CASCADE, **kwargs)

关于python - 类型错误 : __init__() missing 1 required positional argument: 'on_delete' (Django 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940093/

相关文章:

Python 日期时间

python - 在 Django 中,如何为每个模板渲染自动设置 "cache-control"?

python - 在 Windows 7 机器上安装 PyMySQL

Python dict.get 返回 0 评估为 false

python - 如何使用 np.genfromtxt 导入相同列名的数据?

python - 使用正则表达式从文本中提取帮助

python : How to call a global function from a imported module

python - 从 ImageGrab 定位黑色像素的位置

python - 立即加载 django 自定义模板标签

python-3.x - 使用 cnn pytorch 可变长度的文本