问题
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