AttributeError:在Django中使用ArrayField时,“ __ proxy__”对象没有属性“ set_attributes_from_name”

时间:2018-12-08 19:19:52

标签: 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,如果是,为什么? ArrayFields不是关系。

1 个答案:

答案 0 :(得分:2)

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

private void process_OutputCmd(object sender, DataReceivedEventArgs arg)
{
    MessageBox.Show(arg.Data);
}

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C ping 127.0.0.1";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputCmd);
process.Start();
process.BeginOutputReadLine();

所以要存储在数组中的项目的类型。

因此您可以构造如下字段:

class ArrayField(base_field, size=None, **options)

或者您可以显式命名from django.utils.translation import gettext_lazy as _ class MyModel(models.Model): choices = ArrayField( models.CharField(max_length=255), verbose_name=_('choices'), blank=True, null=True, help_text=_('Comma-delimited list.') )base_field,那么顺序无关紧要。