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

3

您初始化嵌套字段的方式是错误的 - 您需要将类而不是类的实例作为参数传递给构造函数(即,去掉括号):

updation = Nested(UpdationIndex)  # Instead of Nested(UpdationIndex())
于 2018-04-07T02:43:51.993 回答