在模型中我已指定对象描述如何从python django后端到AJAX的前端描述对象?
def __unicode__(self): ...............................
而且我希望看到这个描述在使用Ajax生成我的动态投递。 但我只是在下拉菜单中获得[object Object]。在以下方式
我的数据流:
1-I具有消毒器在我的API
class LeaseTermSerializer(serializers.ModelSerializer):
class Meta:
model=LeaseTerm
fields = '__all__'
2-I有鉴于API方法
@api_view(['GET']) @csrf_exempt def get_leaseterm(request, tid):
leasetermobj = LeaseTerm.objects.filter(lease=tid,is_active = True)
leaseterm_serializer = LeaseTermSerializer(leasetermobj, many=True)
response = Response(leaseterm_serializer.data)
return Response(response.data,status=status.HTTP_200_OK)
-3-以我模板我这样构建它
function getleaseterm() {
//get a reference to the select element
$select = $('#leaseterm');
//request the JSON data and parse into the select element
var l_id = ($("select[name='lease'] option:selected").attr('value'));
l_url = "/api/get_leaseterm/"+l_id+"/";
$.ajax({
url: l_url,
dataType:'JSON',
success:function(data1){
//clear the current content of the select
$select.empty();
$select.append('<option value="-1">Select term </option>');
//iterate over the data and append a select option
$.each(data1, function(key, val){
$select.append('<option value="' + val.id + '">' + val + '</option>');
})
},
});
}
问题是,如果我不指定要显示的字段,则在下拉菜单中显示的“val”值将在我希望显示我指定的对象的描述时,为下拉列表中的所有值显示[对象对象]我的模型。
如何在我的下拉列表中查看我的对象描述?