Python 覆盖django管理的正确方法';s add_view()

Python 覆盖django管理的正确方法';s add_view(),python,django,django-views,django-admin,Python,Django,Django Views,Django Admin,我需要覆盖django admin中的视图add_view(),每当我尝试添加新模型实例时都会调用该视图 我所尝试的: class BaseMarketModelAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): obj.creator = request.user return super().save_model(request, obj, form, c

我需要覆盖django admin中的视图
add_view()
,每当我尝试添加新模型实例时都会调用该视图

我所尝试的:

class BaseMarketModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.creator = request.user
        return super().save_model(request, obj, form, change)

    def add_view(self, request, form_url='', extra_context=None):
        try:
            super(BaseMarketModelAdmin, self).add_view(
                request, form_url, extra_context
            )
        except ValidationError as e:
            return handle_exception(self, request, e)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        try:
            return super(BaseMarketModelAdmin, self).change_view(
                request, object_id, form_url, extra_context
            )
        except ValidationError as e:
            return handle_exception(self, request, e)
change\u view()
可以正常工作,但当我尝试使用django admin中的“add ModelName”按钮添加新模型实例时,总是会出现以下异常:

AttributeError at /admin/market/exchange/add/
'NoneType' object has no attribute 'has_header'
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/market/exchange/add/
Django Version: 3.0.3
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'has_header'
Exception Location: /usr/local/lib/python3.7/site-packages/django/utils/cache.py in patch_response_headers, line 243
Python Executable:  /usr/local/bin/python
Python Version: 3.7.7
AttributeError at /admin/market/exchange/add/
'NoneType' object has no attribute 'COOKIES'
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/market/exchange/add/
Django Version: 3.0.3
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'COOKIES'
Exception Location: /usr/local/lib/python3.7/site-packages/django/middleware/csrf.py in _get_token, line 170
Python Executable:  /usr/local/bin/python
Python Version: 3.7.7
我试着检查django的
add_view()
的源代码,它位于:中,似乎只调用
change_view()
,没有对象id。然后我尝试了以下方法:

def add_view(self, request, form_url='', extra_context=None):
    return self.changeform_view(request, None, form_url, extra_context)
def add_view(self, request, form_url='', extra_context=None):
    return BaseMarketModelAdmin.changeform_view(request, None, form_url, extra_context)
并且它正确加载新实例页面,但不调用my
BaseMarketModelAdmin.change\u view()
view

然后我试了一下:

def add_view(self, request, form_url='', extra_context=None):
    return self.changeform_view(request, None, form_url, extra_context)
def add_view(self, request, form_url='', extra_context=None):
    return BaseMarketModelAdmin.changeform_view(request, None, form_url, extra_context)
但它导致了这一例外:

AttributeError at /admin/market/exchange/add/
'NoneType' object has no attribute 'has_header'
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/market/exchange/add/
Django Version: 3.0.3
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'has_header'
Exception Location: /usr/local/lib/python3.7/site-packages/django/utils/cache.py in patch_response_headers, line 243
Python Executable:  /usr/local/bin/python
Python Version: 3.7.7
AttributeError at /admin/market/exchange/add/
'NoneType' object has no attribute 'COOKIES'
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/market/exchange/add/
Django Version: 3.0.3
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'COOKIES'
Exception Location: /usr/local/lib/python3.7/site-packages/django/middleware/csrf.py in _get_token, line 170
Python Executable:  /usr/local/bin/python
Python Version: 3.7.7

现在我需要覆盖
add_view()
view。正确的方法是什么?

add\u view
方法中缺少返回值

    def add_view(self, request, form_url='', extra_context=None):
    try:
        return super(BaseMarketModelAdmin, self).add_view(
            request, form_url, extra_context
        )
    except ValidationError as e:
        return handle_exception(self, request, e)

我可以问一下您想如何处理
添加视图(…)
?@Arakkal我需要手动处理可能引发的异常。与change_view()完全一样,验证也在add_view(…)中管理,不是吗?正确!我不知道我到底怎么会犯这样的错误!!