私信  •  关注

Rustam Garayev

Rustam Garayev 最近创建的主题
Rustam Garayev 最近回复了
3 年前
回复了 Rustam Garayev 创建的主题 » python中从公共密钥字典到单个字典的转换

只需在查看原始词典的值时更新最终词典:

req = {"main1": 
    {"x": {"a":220},"y": {"b":66}},
    "main2": 
    {"x": {"c":"1000","d":"copper"},
    "y": {"c":"1200","d":"Copper"}}}

final_dict = {'cable1': {}, 'cable2': {}}
for v in req.values():
    final_dict['cable1'].update(v['x'])
    final_dict['cable2'].update(v['y'])

最后的口述如下:

{'cable1': {'a': 220, 'c': '1000', 'd': 'copper'}, 'cable2': {'b': 66, 'c': '1200', 'd': 'Copper'}}
3 年前
回复了 Rustam Garayev 创建的主题 » 仅使用字符串作为键更改Django字段

你可以用 **<dict_name> ( dictionary unpacking )要就地更新模型字段值,请执行以下操作:

User.objects.filter(id=user_id).update(**changes)