How to create object from QueryDict in django?

本秂侑毒 提交于 2019-12-13 02:04:28

问题


Let's say I got a QueryDict in my POST request, and I'd like to create a new record in database according to this dict.

QueryDict: {u'phone': [u'Phone'], u'email': [u'Email'], u'full_name': [u't54'], u'skype': [u'Skype']}

Can I do it in one command? What's the best way to go?

Thanks


回答1:


As long as you don't have multiple values for the same key, you can do:

values = QueryDict.dict()

if values:
    YourModel.objects.create(**values)

However, I would strongly suggest using a ModelForm to sanitize the data from the post, and then create the object.




回答2:


Model.objects.create(**QueryDict.dict())


来源:https://stackoverflow.com/questions/30699794/how-to-create-object-from-querydict-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!