python - 升级到 django 5.0 "AttributeError: ' BlankChoiceIterator' 对象没有属性 '__len__' 后出现问题"

标签 python django

我是 django triad 的新手,要升级到版本 5.0,但在我的项目中遇到了这个问题

Traceback (most recent call last):
  File "dirto/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "dirto/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
  File "dirto/env/lib/python3.10/site-packages/django/template/response.py", line 114, in render
    self.content = self.rendered_content
  File "dirto/env/lib/python3.10/site-packages/django/template/response.py", line 92, in rendered_content
    return template.render(context, self._request)
  File "dirto/env/lib/python3.10/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/loader_tags.py", line 159, in render
    return compiled_parent._render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
 File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/defaulttags.py", line 325, in render
    return nodelist.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/defaulttags.py", line 325, in render
    return nodelist.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/defaulttags.py", line 325, in render
    return nodelist.render(context)
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "dirto/env/lib/python3.10/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "dirto/env/lib/python3.10/site-packages/crispy_forms/templatetags/crispy_forms_field.py", line 125, in render
    return str(field)
  File "dirto/env/lib/python3.10/site-packages/django/forms/utils.py", line 79, in __str__
    return self.as_widget()
  File "dirto/env/lib/python3.10/site-packages/django/forms/boundfield.py", line 95, in as_widget
    attrs = self.build_widget_attrs(attrs, widget)
  File "dirto/env/lib/python3.10/site-packages/django/forms/boundfield.py", line 270, in build_widget_attrs
    widget.use_required_attribute(self.initial)
  File "dirto/env/lib/python3.10/site-packages/django/forms/widgets.py", line 781, in use_required_attribute
    first_choice = next(iter(self.choices), None)
  File "dirto/env/lib/python3.10/site-packages/django_countries/widgets.py", line 29, in get_choices
    self._choices: ChoiceList = list(self._choices)
  File "dirto/env/lib/python3.10/site-packages/django/utils/functional.py", line 188, in __wrapper__
    return getattr(result, __method_name)(*args, **kw)
AttributeError: 'BlankChoiceIterator' object has no attribute '__len__'

我向chatgpt寻求帮助,但不太明白 它给了我这个答案

您遇到的错误 AttributeError: 'BlankChoiceIterator' object has no attribute 'len' 可能与 Django 5.0 中的更改有关,特别是与 forms.ChoiceField 及其对选择的处理有关.

在 Django 5.0 中,ChoiceField 中的 Choices 参数的默认行为已更改。现在,它要求选择是元组的可迭代,并且每个元组的第一个元素应该是唯一的。

最佳答案

如果您使用填充该字段的包,您将收到此错误。我将使用 django-countries 作为示例。如果您使用类似的库,您希望获取实际的选择并将其传递到带有选择参数的 models.CharField 中并调用选择。让我知道这是否可以解决问题。如果您能发布您的模型或表格,那就太好了

from django_countries.fields import CountryField


# Create your models here.

class BaseModel(models.Model):
    # ... other fields

    country = models.CharField(max_length=200,  null=True, choices=CountryField().choices + [('', 'Select Country')])

    class Meta:
        abstract = True`

关于python - 升级到 django 5.0 "AttributeError: ' BlankChoiceIterator' 对象没有属性 '__len__' 后出现问题",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77667419/

相关文章:

python - 从 Python 中数据框中的时间戳列计算唯一的工作日

python - Django模型字段按变量

python - ValueError 具有多个元素的数组的真值不明确 使用 any 或 all

python - Pydev:如何将gae项目导入到eclipse Pydev gae项目中?

django - 限制django表单文件上传的文件扩展名

html - 如何强制导航栏中的下拉 Bootstrap 按钮填充整个容器宽度?

javascript - 根据控制台未注册 Controller

django - 以编程方式创建 Page 对象时出现错误 ValidationError 路径和深度字段不能为空/空

python - IdeaVim 与 PyCharm 上的 Jupyter Notebook

python - 使用Python以MySQL json列格式插入Json数据