Reputation: 437
MY VIew
def cargar_categorias(request):
data = serializers.serialize("json", MyModel.objects.all())
return HttpResponse(data, mimetype='aplication/json')
response json
[{"pk": 8, "model": "MyModel.MyModel", "fields": {"celular": "234342342", "logo": "recetas/aurelio.jpg", "tipo": 1, ....
but the field "tipo" is a foreign Key, I need recover the value of this field and not the id (1)
My Jquery code
$.ajax({
type: "GET",
contentType: "application/json; charset-utf-8",
dateType: "json",
url: "/cargar_categorias",
success: function(response){
var i=(response.length-1)-6;
for( i;i<=(response.length-1);i++){
$('#locations').append("<li data-celular='"
+ response[i].fields.celular +"' data-tipo='"
+ response[i].fields.tipo +"'><img href=#"
+ response[i].fields.logo +" src='/media/"
+ response[i].fields.logo +"'/></li>");
}
}
});
I think can use select_related() but not work, or response[i].fields.tipo.nombre or .... I don't now, please help me, this going crazy, sorry my english
Upvotes: 0
Views: 1404
Reputation: 5390
One potential way around this is to construct your own dictionary object based on the returns of a queryset. You'd do something like this:
queryset = MyModel.objects.all()
list = [] #create list
for row in queryset: #populate list
list.append({'celular':row.celular, 'logo': row.logo, 'tipo': row.tipo.id})
list_json = json.dumps(list) #dump list as JSON
return HttpResponse(list_json, 'application/javascript')
Upvotes: 1