2017-05-14 10 views
1

配列フィールドを使用して、特定のカスタムクラスを表すjson文字列のリストを格納します。私が行っていることである。カスタムフィールド値を格納するArrayField

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' 

任意の考えI間違っている可能性がありますか? おかげ

答えて

0

スタンドアロンフィールドとしてbase_field属性を定義しよう - MyCustomField() istead MyCustomFieldの:

pres = ArrayField(MyCustomField())

関連する問題