ArrayField缺少1个必需的位置参数

时间:2016-12-16 09:12:45

标签: python django

我导入了以下头文件

from django.contrib.postgres.fields import ArrayField

在模型中使用以下内容

question_array = ArrayField(models.IntegerField, blank=True,)

我收到以下错误

Traceback (most recent call last)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files (x86)\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Users\DELL\Desktop\cstrom\comp\models.py", line 24, in <module>
    class User(models.Model):
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 157, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\fields\__init__.py", line 689, in contribute_to_class
    self.set_attributes_from_name(name)
  File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\contrib\postgres\fields\array.py", line 75, in set_attributes_from_name
    self.base_field.set_attributes_from_name(name)
TypeError: set_attributes_from_name() missing 1 required positional argument: 'name'

2 个答案:

答案 0 :(得分:9)

根据docs

您仍需要在数组字段

中完全定义字段
question_array = ArrayField(models.IntegerField(null=True, blank=True), blank=True,)

答案 1 :(得分:3)

models.IntegerField是一个函数而不是属性,因此您需要像函数一样调用它。最后()

question_array = ArrayField(models.IntegerField(blank=True), blank=True,)

详细了解here