带有自定义字符串id字段的Django模型未被删除

tktrz96b  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(82)

我有一个模型,其中有一个ID的自定义字符串。

class BaseModel(Model):
    id = CharField(
        max_length=23,
        auto_created=True,
        primary_key=True,
        serialize=False,
        verbose_name="ID",
        default=generate,
        editable=False,
    )

删除时,我得到这个错误。

Internal Server Error: /admin/sampleapp/user/
Traceback (most recent call last):
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1988, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'AC7EEKrEhYZ9p1r-Q'

The above exception was the direct cause of the following exception:

    response = func(self, request, queryset)
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\actions.py", line 39, in delete_selected
    ) = modeladmin.get_deleted_objects(queryset, request)
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\options.py", line 2099, in get_deleted_objects
    return get_deleted_objects(objs, request, self.admin_site)
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 120, in get_deleted_objects
    collector.collect(objs)
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 186, in collect
    return super().collect(objs, source_attr=source_attr, **kwargs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 343, in collect
    field.remote_field.on_delete(self, field, sub_objs, self.using)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 23, in CASCADE
    collector.collect(
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\admin\utils.py", line 186, in collect
    return super().collect(objs, source_attr=source_attr, **kwargs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\deletion.py", line 365, in collect
    sub_objs = field.bulk_related_objects(new_objs, self.using)
  File "path\to\sampleapp\venv\lib\site-packages\django\contrib\contenttypes\fields.py", line 524, in bulk_related_objects
    return self.remote_field.model._base_manager.db_manager(using).filter(
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1071, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1089, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\query.py", line 1096, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1502, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1448, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\sql\query.py", line 1273, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\lookups.py", line 27, in __init__
    self.rhs = self.get_prep_lookup()
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\lookups.py", line 276, in get_prep_lookup
    rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
  File "path\to\sampleapp\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1990, in get_prep_value
    raise e.__class__(
ValueError: Field 'object_pk' expected a number but got 'AC7EEKrEhYZ9p1r-Q'.
"POST /admin/sampleapp/user/ HTTP/1.1" 500 213503

请帮助解决这个问题。
我第一次注意到这个问题时,试图通过管理删除,但没有工作。检查后,我注意到的问题是,该模型无法删除,甚至通过 shell 。

0dxa2lsx

0dxa2lsx1#

从您的回溯:

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

它需要一个整数。是否有特殊的原因需要替换id字段?您可以为自定义字段创建一个单独的字段,并在程序的其他地方将其用作id

相关问题