如何使用try catch

以下时报错信息,最后一行有错误类型ValueError:

Traceback (most recent call last):
  File "<input>", line 76, in <module>
  File "C:\Users\Administrator\PycharmProjects\first_django\first_app\db_opr.py", line 439, in find_or_create_field_content_in_simple_class
    query_set = the_model.objects.filter(**criteria)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1253, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1277, in _add_q
    split_subq=split_subq,
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1215, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1085, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\lookups.py", line 18, in __init__
    self.rhs = self.get_prep_lookup()
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\lookups.py", line 68, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1826, in get_prep_value
    return int(value)

ValueError: invalid literal for int() with base 10: ''


根据这个错误类型ValueError,可以写try catch原因:

number = None
try:
    # 创建第34列的联系人电话
    number = find_or_create_field_content_in_simple_class(ContactPhoneNumber, {'phonenumber': row[33]})
except ValueError:
    print("插入第" + str(i) + "条数据时,联系人电话为空或格式不对")

阅读更多
上一篇python django中的时区问题 DateTimeField received a naive datetime while time zone support is active
下一篇Model的一列数据,default=None与null=True,如果没有null=True,则不允许该数据为None,相当于default=None不起作用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭