Django ArrayField在语法或附近给出了语法错误

时间:2016-08-26 09:32:41

标签: django django-models

我使用Python3,Django == 1.9.9,psql(PostgreSQL)9.5.4实现ArrayField,但如果我这样做:

[[0, 4.27, 12, 12, 77, 4], [2.01, 3.39, 5, 6, 7, 1]]

我收到此错误,此处附有完整的Traceback:

from django.utils.translation import ugettext_lazy as _
*
*    
exceptions = ArrayField(models.CharField(max_length=200), _('Exceptions'), default=list, help_text=exceptions_help_text)

虽然Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line utility.execute() File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute output = self.handle(*args, **options) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration state = migration.apply(state, schema_editor) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 482, in alter_field old_db_params, new_db_params, strict) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 116, in _alter_field new_db_params, strict, File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 634, in _alter_field params, File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute cursor.execute(sql, params) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "/srv/virtualenvs/myproj/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: syntax error at or near "Exceptions" LINE 1: ..._wms" ALTER COLUMN "exceptions" TYPE varchar(200)[Exceptions... 有效:

makemigrations

这是一个错误吗?

如果不能如何设置字段名称?

1 个答案:

答案 0 :(得分:2)

从链接的代码中可以看出,第二个位置参数是字段的大小,这就是Django试图将它传递给Postgres的原因,而这通常不会对名称进行处理。< / p>

如果要定义详细名称,请将其作为kwarg传递:

exceptions = ArrayField(models.CharField(max_length=200), verbose_name=_('Exceptions'), default=...)