How to create object from QueryDict in django?

1.9k views Asked by At

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

2

There are 2 answers

0
Brandon Taylor On

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.

0
moonstruck On
Model.objects.create(**QueryDict.dict())