dimanche 28 juin 2015

Issue storing multi valued json format dictionary into mysql table in DJango

I have below ajax call in my template

temp.html

function saveprof() {
            $.ajax({
                type: "POST",
                url: "saveprof",
                async: true,
                data: {
                    'radinput_Aj' : fun(),
                    'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
                },
                success: function (data, textStatus, jqXHR) {
                    $('#message').html(data);
                }
            });
        }

fun() returns a json format object v - which is as below :

example :

var v = [
             { axis: "", value: 4, order: 0 },
             { axis: "", value: 4, order: 1 },
             { axis: "", value: 4, order: 2 },
             { axis: "", value: 4, order: 3 },
             { axis: "", value: 4, order: 4 },
             { axis: "", value: 4, order: 5 },
             { axis: "", value: 4, order: 6 }
    ];

Views.py

def saveprof(request):
if request.method == "POST":
    radinputV=[]
    radinputV = request.POST['radinput_Aj']
else:
    response_data = 'Nothing to update!'
    return HttpResponse(response_data, content_type="text/plain")
response_data = ''
try:
*(Here, I would like to insert the incoming dictionary (radinputV) rows into my model skills)*
except:
    response_data = 'Ouch! Something went wrong!'
return HttpResponse(response_data, content_type="text/plain")

Below is the skills model :

class skills(models.Model):
id = models.CharField(max_length=300,blank=True)
skill = models.CharField(max_length=300,blank=True)
rating = models.CharField(max_length=300,blank=True)
ordernum = models.CharField(max_length=300,blank=True)

Here is the mapping of incoming json dictionary from template with the fields in skills model - skill is axis, rating is value, ordernum is order

However, when I run the code, it fails with MultivaluDictKeyError at below line

radinputV=[]
radinputV = request.POST['radinput_Aj']

What could be the problem and how to handle this scenario of inserting multiple rows into mysql table from template JSON object - Django view ?

Edit : I tried using .get() method already in the view.. but I'm not sure how to insert dictionary rows into my skills table.. can you pls help me with the approach ?

Aucun commentaire:

Enregistrer un commentaire