我想同时在博客Web后端的编辑/(添加新帖子)中同时保留ckeditor和markdown,因此我修改了__init__
,此后,我仍然可以打开编辑页面。但是,我单击“保存”按钮,出现此错误:TypeError: __init__() got multiple values for argument 'instance'
Traceback (most recent call last):
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\contrib\admin\options.py", line 604, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\contrib\admin\options.py", line 1640, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\contrib\admin\options.py", line 1525, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "E:\python_works\project\django\typeidea-env\lib\site-packages\django\contrib\admin\options.py", line 1556, in _changeform_view
form = ModelForm(request.POST, request.FILES, instance=obj)
TypeError: __init__() got multiple values for argument 'instance'
这是我删除后不会出现错误的部分:
class PostAdmin(forms.ModelForm):
....
def __init__(self, instance=None, initial=None, **kwargs):
initial = initial or {}
if instance:
if instance.is_md:
initial['content_md'] = instance.content
else:
initial['content_ck'] = instance.content
super().__init__(instance=instance, initial=initial, **kwargs)
....
答案 0 :(得分:0)
我喜欢这里的解决方案:
Django: multiple values for keyword argument 'initial'
通常来说,这是与我相同的问题。某种程度上,instance
不会收到def __init__(self, instance=None, initial=None, **kwargs):
传递给instance
的信息,而是会传递给kwargs
(仍然不会知道为什么),然后intance
默认为None
,所以有两个inastance
参数,第一个实例默认为None
,第二个实例{{1 }}位于instance
中,这是我想传递的真实
以便我做些什么来解决它:
kwargs