0

ネストされたフィールドを持つオブジェクトを保存しようとすると、型エラーが発生します。

TypeError: isinstance() arg 2 must be a type or tuple of types

次のドキュメント クラスがあります。

class CreationIndex(InnerDoc):

    created_by = Keyword()
    created_date = Date()

class UpdationIndex(InnerDoc):

    updated_by = Keyword()
    updated_date = Date()

class IndustryIndex(DocType):
    name = Text(analyzer= 'english')
    creation = CreationIndex()
    updation = Nested(UpdationIndex())

    class Meta:
        index = 'industry'

def bulk_indexing():

    elastic_connection = Elasticsearch(hosts=['localhost'], timeout=20)
    if elastic_connection.indices.exists('industry'):
        elastic_connection.indices.delete('industry')
    if elastic_connection.indices.exists('category'):
        elastic_connection.indices.delete('category')

    IndustryIndex.init()
    print('HI')
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))

私のインダストリ モデルは、次のように定義されている django モデルです (mongoDB を使用しているため、それ自体が動的ドキュメントです)。

class Industry(DynamicDocument,BaseCreationUpdation):
    name=StringField(required=True,unique=True)

    def save(self,*args,**kwargs):
        '''
        '''
        self=save_created_updated_info(self)
        super(Industry,self).save(args,kwargs)

    def indexing(self):
        objCreation = CreationIndex(
            created_by = self.creation.created_by,
            created_date = self.creation.created_date.date().isoformat()
            )
        print(objCreation.to_dict())
        obj = IndustryIndex(
            meta = {'id': str(self.id)},
            name = self.name,
            creation = objCreation.to_dict(),
            updation = [],
            )

        for objUp in self.updation:

            objUpdation = UpdationIndex(
                updated_by = objUp.updated_by,
                updated_date = objUp.updated_date.date().isoformat()
                )
            print(objUpdation)
            obj.updation.append(objUpdation)
            print()
            print(obj.to_dict())
            print()
        obj.save()

        return obj.to_dict(include_meta=True)

    @classmethod
    def index_document(cls,sender, document, **kwargs):
        document.indexing()

今、bulk_indexing モジュールを実行するたびに、問題が発生します。

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\projects\django\bhetincha\category\search.py", line 57, in bulk_indexing
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 257, in bulk
    for ok, item in streaming_bulk(client, actions, **kwargs):
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 180, in streaming_bulk
    client.transport.serializer):
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 58, in _chunk_actions
    for action, data in actions:
  File "D:\projects\django\bhetincha\category\search.py", line 57, in <genexpr>
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))
  File "D:\projects\django\bhetincha\category\models.py", line 80, in indexing
    obj.save()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\document.py", line 405, in save
    self.full_clean()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\utils.py", line 417, in full_clean
    self.clean_fields()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\utils.py", line 403, in clean_fields
    data = field.clean(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 179, in clean
    data = super(Object, self).clean(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 90, in clean
    data = self.deserialize(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 81, in deserialize
    for d in data
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 81, in <listcomp>
    for d in data
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 160, in _deserialize
    if isinstance(data, self._doc_class):
TypeError: isinstance() arg 2 must be a type or tuple of types

なぜこのエラーが発生するのですか? ネストされた更新ファイルを削除すると、作成フィールドは同じですが、すべて機能します。ここで何が問題なのですか?

4

1 に答える 1