我想使用arrayfield来存储代表特定自定义类的json字符串列表。我所做的是:
from django.contrib.postgres.fields import JSONField, ArrayField
from django.db import models
# other imports
class MyCustomField(models.Field):
a = models.FloatField(blank=True, null=True)
b = models.DateTimeField()
c = JSONField(blank=True, null=True)
def db_type(self, connection):
return 'Text'
def rel_db_type(self, connection):
return 'integer UNSIGNED'
def to_python(self, value):
return json.loads(value)
def get_prep_value(self, value):
return json.dumps(value)
class A(models.Model):
# ... various normal fields here, then:
pres = ArrayField(MyCustomField)
def get_absolute_url(self):
return reverse('foo:bar', kwargs={'pk': self.id})
可是:
python manage.py makemigrations
抛出以下错误
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/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'
完整的追溯是:
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "<path_to_my_virtualenv_dir>/lib/python3.5/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 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<path_to_my_app>/models.py", line 62, in <module>
class SolarPrediction(models.Model):
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/db/models/base.py", line 157, in __new__
new_class.add_to_class(obj_name, obj)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 689, in contribute_to_class
self.set_attributes_from_name(name)
File "<path_to_my_virtualenv_dir>/lib/python3.5/site-packages/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'
对我可能做错了什么的任何想法? 感谢
答案 0 :(得分:0)
尝试将base_field
属性定义为独立字段 - MyCustomField()
而不是MyCustomField
:
pres = ArrayField(MyCustomField())