【问题标题】:Correct way to override django admin's add_view()覆盖 django admin 的 add_view() 的正确方法
【发布时间】:2020-11-22 02:32:46
【问题描述】:

我需要覆盖 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_view() 工作没有任何问题,但是当我尝试使用 django admin 中的“添加模型名称”按钮添加新模型实例时,我总是得到这个异常:

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

我尝试检查 django 的 add_view() 的源代码,它位于:django/contrib/admin/options.py,它似乎只调用了没有 object_id 的 change_view()。然后我尝试了这个:

def add_view(self, request, form_url='', extra_context=None):
    return self.changeform_view(request, None, form_url, extra_context)

它会正确加载新的实例页面,但不会调用我的BaseMarketModelAdmin.change_view() 视图!

然后我尝试了这个:

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 '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() 视图。这样做的正确方法是什么?

【问题讨论】:

  • 请问你想用add_view(...)做什么?
  • @Arakkal 我需要手动处理可能引发的异常。和 change_view() 完全一样
  • 验证也在 add_view(...) 中管理,不是吗?

标签: python django django-views django-admin


【解决方案1】:

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

【讨论】:

  • 正确!我不知道我到底怎么会犯这样的错误!
猜你喜欢
  • 2020-05-03
  • 2016-07-26
  • 2011-05-10
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 2017-05-26
  • 2016-10-19
  • 1970-01-01
相关资源
最近更新 更多