django - 属性错误: '__proxy__' object has no attribute 'set_attributes_from_name' when using ArrayField in Django

标签 django django-models

我尝试在模型中为 ArrayField 设置延迟翻译。像这样的事情:

from django.utils.translation import gettext_lazy as _

class MyModel(models.Model):
    choices = ArrayField(
        _('choices'),
        models.CharField(max_length=255),
        blank=True,
        null=True,
        help_text=_('Comma-delimited list.')
    )

但是,我收到此错误:

AttributeError: '__proxy__' object has no attribute 'set_attributes_from_name'

应该改为verbose_name吗?如果是,为什么? ArrayField 不是关系。

最佳答案

ArrayField [Django-doc] 的第一个参数是:

class ArrayField(<b>base_field</b>, size=None, **options)

因此,将存储在数组中的项目的类型。

所以你可以构造这样的字段:

from django.utils.translation import gettext_lazy as _

class MyModel(models.Model):
    choices = ArrayField(
        models.CharField(max_length=255),
        <b>verbose_name=_('choices'),</b>
        blank=True,
        null=True,
        help_text=_('Comma-delimited list.')
    )

或者您可以显式命名 base_fieldverbose_name,那么顺序并不重要。

关于django - 属性错误: '__proxy__' object has no attribute 'set_attributes_from_name' when using ArrayField in Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686071/

相关文章:

python - Django读取excel(xlsx)文件以显示在表格中

python - 将模型范围的帮助文本添加到 django 模型的管理表单

在 url 中使用点的 Django Rest Framework

python - 在 Django 中有条件地扩展模板

python - 如何使用 Django 消息框架发送 Json 或 "dict"

django - 如何使用 (HTMX) 更新两个 HTML 模板元素?

django - 从 ManyToMany 查询集中序列化数据

python - celery 会默认拒绝接受 pickle ,我应该禁用它吗?

python - 有没有办法指定接受 __init__ 值的抽象函数

Django 表单; 'bool' 对象没有属性 'get'