[英]Error when returning single json object after an ajax call in django
当我有多个对象时,它工作正常,但是如果我只有1个,它将返回错误
views.py
def get_incident_personnel(request):
args = {}
store = Store.objects.get(store_id=request.POST['store'])
data = serializers.serialize('json', store)
return HttpResponse(data, mimetype='application/json')
这是我的ajax.js
$('#id_store').change(function() {
var store = $('#id_store').val();
$.ajax({
url: '/incidents/get_incident_personnel/',
type: 'POST',
dataType: 'json',
data: {
'store': store,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
success: function(data) {
console.log(data);
}
});
});
您不能仅在QuerySet上对模型实例使用serializers.serialize
。 使用filter
即使您知道只会得到一个对象。
store = Store.objects.filter(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.