gpt4 book ai didi

django - ArrayField 存储自定义字段值

转载 作者:行者123 更新时间:2023-12-05 06:39:59 24 4
gpt4 key购买 nike

我想使用数组字段来存储代表特定自定义类的 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'

关于我可能做错了什么的想法?谢谢

最佳答案

尝试将 base_field 属性定义为独立字段 - MyCustomField() 而不是 MyCustomField:


pres = ArrayField(MyCustomField())

关于django - ArrayField 存储自定义字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43969070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com