Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复此错误:+;的操作数类型不受支持:';记录项目';和';odict#U项目';?_Python_Django_Dictionary - Fatal编程技术网

Python 如何修复此错误:+;的操作数类型不受支持:';记录项目';和';odict#U项目';?

Python 如何修复此错误:+;的操作数类型不受支持:';记录项目';和';odict#U项目';?,python,django,dictionary,Python,Django,Dictionary,我试图在django 2.0中实现 def get_dump_object(self, obj): metadata = { "pk": smart_text(obj._get_pk_val(), strings_only=True), "model": smart_text(obj._meta), } return dict(metadata.items() + self._current.items()) 但我得到了这个错误: uns

我试图在django 2.0中实现

def get_dump_object(self, obj):
    metadata = {
        "pk": smart_text(obj._get_pk_val(), strings_only=True),
        "model": smart_text(obj._meta),
    }
    return dict(metadata.items() + self._current.items())
但我得到了这个错误:

unsupported operand type(s) for +: 'dict_items' and 'odict_items'

如何合并普通dict和有序dict?

在尝试相互添加时,不能将字典与
+
一起使用(与支持
+
运算符的列表不同)

改用:

或者:

In [52]: from collections import OrderedDict
    ...: o = OrderedDict([(1,2), (3, 4)])
    ...: d = {5:6, 7:8}
    ...: 

In [53]: d.update(o); print(d)
{1: 2, 3: 4, 5: 6, 7: 8}

当试图相互添加时,不能将
+
与词典一起使用(与支持
+
运算符的列表不同)

改用:

或者:

In [52]: from collections import OrderedDict
    ...: o = OrderedDict([(1,2), (3, 4)])
    ...: d = {5:6, 7:8}
    ...: 

In [53]: d.update(o); print(d)
{1: 2, 3: 4, 5: 6, 7: 8}

您可以使用
或运算符
|


您可以使用
或运算符
|


充实你的问题,你正在运行的代码是什么?我认为,如果你在这个问题中添加代码会更好。你的字典包含什么?整数、浮点数、字符串、混合数?充实你的问题更多,你正在运行的代码是什么?我认为,如果你在这个问题中添加代码会更好。你的字典包含什么?整数、浮点数、字符串、混合数?
metadata = {
    "pk": smart_text(obj._get_pk_val(), strings_only=True),
    "model": smart_text(obj._meta),
    "title": smart_text(obj.training_type.name)
}
return dict(metadata.items() | self._current.items())