django order_by FieldError异常无法捕获 [英] django order_by FieldError exception can not be catched

查看:105
本文介绍了django order_by FieldError异常无法捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from django.core.exceptions import FieldError

#This is a method of a class
def _order_item_list(self, item_list, order_items_by, previous_order_by):
    if order_items_by == previous_order_by:
        order_items_by = '-' + order_items_by 

    try:
        result = item_list.order_by(order_items_by)
    except FieldError:
        result = item_list

    return result, order_items_by

现在,当我按照生成的链接后的有效字段进行排序时,一切都可以正常运行。当我编辑链接并添加一些虚拟字段名以进行排序时,此异常应将其捕获,并应返回原始列表。但这并没有发生,相反,我总是从Django收到FieldError。

Now when I order by valid fields following the generated link,everything works perfect. When I edit a link and add some dummy fieldnames for ordering, it should be catched by this exception and the original list should be returned. But it is not happening, instead I always get a FieldError from django.

FieldError at ...

FieldError at ...

无法将关键字u'fgsdffds'解析为字段。选择包括:...

Cannot resolve keyword u'fgsdffds' into field. Choices are: ...

推荐答案

这表示存在错字,否则在其他地方会发生异常。插入调试行:

This means there's a typo, or the exception happens elsewhere. Insert a debug line:

 import pdb; pdb.set_trace()

,然后看看如何执行代码。尝试 PUDB IPDB 调试器,而不是标准的调试器。当您拥有调试器时,许多问题都会消失,并且可以确切地看到出了什么问题。

before the try-except and see how the code is executed. Try PUDB or IPDB debuggers instead of the standard one. Many questions disappear when you have a debugger and can see exactly what goes wrong.

这篇关于django order_by FieldError异常无法捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆