将数据保存在字段名称来自变量的模型中
Save data in Models where field name comes from a variable
我想将数据保存在模型中,其中字段名存储在变量中,但在存储时出现 invalid keyword argument
错误
我的代码:
field = request.POST['creationLanguage']
title = Translation.objects.create(field = request.POST['title'])
此处字段存储模型翻译的字段名称,但我如何使用此动态存储数据field_name。
使用kwargs
魔法:
field = request.POST['creationLanguage']
value = request.POST['title']
title = Translation.objects.create(**{field: value})
我想将数据保存在模型中,其中字段名存储在变量中,但在存储时出现 invalid keyword argument
我的代码:
field = request.POST['creationLanguage']
title = Translation.objects.create(field = request.POST['title'])
此处字段存储模型翻译的字段名称,但我如何使用此动态存储数据field_name。
使用kwargs
魔法:
field = request.POST['creationLanguage']
value = request.POST['title']
title = Translation.objects.create(**{field: value})