即使我传入对象,Django仍然没有类型列表?(许多字段很多)

问题描述 投票:0回答:1

我正在尝试为我的django应用程序运行迁移脚本,但是即使我显然传入了一个对象,我仍然得到TypeError: unhashable type: 'list'

我得到:

 error line 87, in buildCisc
    c.exclusions.add(exclus)
line 944, in add
    self._add_items(
line 1119, in _add_items
    target_ids = self._get_target_ids(target_field_name, objs)
line 1059, in _get_target_ids
    target_ids.add(target_id)
TypeError: unhashable type: 'list'

当我运行以下代码时

...

        for ex in exclusionList:
            if len(Course.objects.filter(ID=ex)) > 0:  # exclusion course already exsists
                exclus = Course.objects.filter(ID=ex)
                c.exclusions.add(exclus[0])
            else:  # exclusion course does not exsist yet so we must create it first
                exclus = Course(ID=ex)
                exclus.save()
                c.exclusions.add(exclus) #this is line 87 causing the error

其中c是在先前代码中创建的Course对象,exclusions是从Course到自身的多对多字段,“ ex”只是一个字符串。如果我尝试使用exclus = Course.objects.create(ID=ex)代替,也会产生相同的错误。错误似乎是因为我要传递给c.exclusions.add的专有内容是列表,但很显然它是一个对象。我什至尝试将排他性切换为排他性[0],以查看它是否以某种方式认为是列表,但这给了我error: Course Object not iterable,因此它说它的类型是Object,所以我对错误消息感到非常困惑。有什么想法吗?

python django django-migrations django-database
1个回答
0
投票

因为c是Course对象,所以它包含一个列表,并且这是不能散列的部分。尝试使用.append而不是.add

© www.soinside.com 2019 - 2024. All rights reserved.