Python 如何修复此错误:+;的操作数类型不受支持:';记录项目';和';odict#U项目';?
我试图在django 2.0中实现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
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())