Serialize a .values () as json. showing data from two tables

0

I want to represent the result in json of some values of the queryset but I could not, it throws me an error that the object dict has no meta class.

'dict' object has no attribute '_meta'

I am using this code:

def listado_libros(request):
    cons=Libros.objects.values("nombre","autores_libros__nombre")
    jsonres=serializers.serialize("json",cons)
    return HttpResponse(jsonres, content_type="application/json")

I know that the json expects a queryset and not result values of a queryset, but the detail is that if I place .only as I have read in a forum of this same web, at the end the result is the same.

1   
model   "migue.libros"
pk  10
fields  
nombre  "x-men"
autores 
0   8
2   
model   "migue.libros"
pk  11
fields  
nombre  "lost"
autores 
0   7
1   8

I would like to attach to that answer json the name of the authors instead of the id but the form I have obtained is with values, the other way that I tried was going through the variable cons with a for and showing with variable.autores_libros.all #authors_books is my related name in the model. and I get the results but I do not know how to represent that process to json because I can store it in a variable but the serializer expects a queryset and throws me error too: L

I want a result like this:

1   
model   "migue.libros"
pk  10
fields  
nombre  "x-men"
autores 
0   "miguel"
2   
model   "migue.libros"
pk  11
fields  
nombre  "lost"
autores 
0   "miguel"
1   "jose"

Using the queryset Book.objects.all () it returns the same result with the ids because it is the related field but I want to access the other attributes. Thanks in advance.

    
asked by miguel 08.09.2017 в 07:21
source

0 answers