【问题标题】:How can I delete or update an object that has TypeError?如何删除或更新具有 TypeError 的对象?
【发布时间】:2019-10-29 21:54:47
【问题描述】:

我的应用程序创建了一个错误的对象,用户插入了一个字符串(我相信),但该字段是十进制。

我知道表单会阻止它,但插入发生在 post_save 装饰器上,现在我无法访问、更新或删除该对象。

comandas_antecipadas = ComandaAntecipado.objects.all()
comandas_antecipadas
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 244, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 268, in __iter__
    self._fetch_all()
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 63, in __iter__
    for row in compiler.results_iter(results):
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1009, in apply_converters
    value = converter(value, expression, connection)
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 254, in converter
    return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
TypeError: argument must be int or float

comandas_antecipadas.count()
11
comandas_antecipadas[9]
<ComandaAntecipado: ComandaAntecipado object (120)>
comandas_antecipadas[10]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 302, in __getitem__
    qs._fetch_all()
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\query.py", line 63, in __iter__
    for row in compiler.results_iter(results):
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1009, in apply_converters
    value = converter(value, expression, connection)
  File "C:\projetos\barzim\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 254, in converter
    return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
TypeError: argument must be int or float

【问题讨论】:

  • 您可以尝试通过数据库的 sql 控制台删除对象。警告:在尝试此操作之前,请备份数据库。在直接在生产环境中尝试之前,您还应该对暂存环境中的数据库快照执行此操作。
  • 不可能将字符串存储在声明为十进制的数据库列中。
  • @Barmar 它位于诸如 sqlite 之类的数据库中,它不强制列类型。
  • 谢谢@AnuvratParasha,我使用 SQLite 浏览器备份并删除了它,它成功了!

标签: python django python-3.x typeerror


【解决方案1】:

当您尝试在控制台中显示对象时,您的问题正在发生。所以,不要那样做。直接删掉就好了:

comandas_antecipadas = ComandaAntecipado.objects.all()
comandas_antecipadas[9].delete()

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2022-07-21
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多