首页 > 解决方案 > isinstance() arg 2 must be an type or tuple of types error in Django

问题描述

使用 Django Rest Framework 时,我不断收到此错误:

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

这是我的模型:

class mymodel(models.Model):
    firstfield = models.ArrayModelField(models.FloatField())
    secondfield = models.ArrayModelField(models.FloatField())

    def save(self, *args, using=None, **kwargs):
        super(mymodel, self).save(*args, using='mydb', **kwargs)

这是我的数据的样子:

id: 'some id here'
firstfield: ['somefloat', 'somefloat'], ['another float', 'another float'] ...
secondfield: ['somefloat', 'somefloat'], ['another float', 'another float'] ...

我认为问题出在我的 MongoDB 数据上。基本上,firstfield'secondfield' 都是列表,包含其他列表,每个列表都有两个浮点数。解决此问题的每一个建议都值得赞赏

标签: pythondjangomongodbdjango-modelsdjango-rest-framework

解决方案


ArrayModelField[Djongo-doc]上的文档说:

使用 MongoDB,在父文档中可以有一组嵌入文档。您可以在父模型中创建嵌入数组/模型列表并将其直接存储到 MongoDB 中。

所以这用于存储模型对象的集合,而不是浮点数等。您可以为此使用ListField[Djongo-doc]


推荐阅读