represent in Json a filter (). values ()

1

Friends, I've been trying to represent this in json but I could not

miguel <QuerySet [<Libro: hunger games>, <Libro: sherlock holmes
jose <QuerySet [<Libro: divergente>, <Libro: sherlock holmes>]>

this is the code that generates that result that I want to represent in json

def listado_libro(request):
    consulta1=Autor.objects.all().prefetch_related("autores_libros")
    for l in consulta1:
        print(l.nombre,l.autores_libros.all())

    consultajson= serializers.serialize("json",consulta1,fields=("nombre","autores"))
    return HttpResponse(consultajson,content_type="application/json")

in a few words based on these models

class Autor(models.Model):
    nombre=models.CharField(max_length=10)
    apellido=models.CharField(max_length=10)
    cedula=models.CharField(max_length=8,unique=True)
    def __str__(self):
        return self.nombre

class Libro(models.Model):
    nombre=models.CharField(max_length=20)
    autores=models.ManyToManyField(Autor,related_name="autores_libros")
    def __str__(self):
        return self.nombre

class Tienda(models.Model):
    nombre=models.CharField(max_length=20)
    libros=models.ForeignKey(Libro, related_name="libros_tienda")
    autores=models.ForeignKey(Autor,related_name="autores_tienda")
    def __str__(self):
        return self.nombre

I want the answer json to be the books along with the names of their authors. so far I have not been able to represent the values of the related table, I have tried with Autor.objects.values("nombre","autores__nombre") and I get the error that the dict object has no meta attribute.

my result if Autor.objects.only instead of values does not show me the values of the name I have not been able to show values from another table in response json. I have achieved it in python as such by printing with a for loop and in the database api with .values. thanks in advance

    
asked by Miguel 08.09.2017 в 17:42
source

1 answer

0

Thanks Cecilio Alonso, I am very helpful with your suggestion. I share the result so that I can send help to another apprentice user hehe.

def listado_libro(request):
    consulta1=Libro.objects.values("nombre","autores__nombre")
    consultajson=json.dumps(list(consulta1))
    return HttpResponse(consultajson,content_type="application/json")

which yields this result in the json path

0   
nombre  "hunger games"
autores__nombre "miguel"
1   
nombre  "divergente"
autores__nombre "jose"
2   
nombre  "sherlock holmes"
autores__nombre "miguel"
3   
nombre  "sherlock holmes"
autores__nombre "jose"

I told you to test Autores__nombre and it did not work, although I knew that it would be wrong to put the name of the model as it would not work, I have been reading the documentation django to know why they require the name of the model in lowercase when you want to use it as a basis to follow a field from another related table. the fact is that it works, I appreciate it.

Now what I have left to do I see it easier than it is to place in a single record the two authors, by way of organization and for a better visualization, that the book that has two or more authors shows it in a single record json In this case, "Sherlock Holmes" does not repeat itself, unless it says in its registry that it is anchored to two authors: Miguel and José. but what I learned is already an advance, thank you very much.

    
answered by 08.09.2017 в 22:31