Get related fields Django

1

Good day how can I bring the related fields from another table in django, for example I have two models Person and Location since when doing the queryset it only brings me the keys, but it does not bring all the fields of the related table.

my code in mysql

('SELECT * FROM localidad inner join Persona on(localidad.idlocalidad=Persona.localidad_idlocalidad)'):

obj = Localidad.objects.get(pk=511)
obj.persona_set.all()
>>> <QuerySet [<Persona: Persona object>]>

I want to bring the other fields, not only the primary key, such as telephone, age and others, but I can only bring the one in return.

    
asked by Andres Mejia 11.11.2017 в 14:46
source

1 answer

1

You can access the rest of the fields in the following way:

 obj = Localidad.objects.get(pk=511)
 personas = obj.persona_set.all()
 for persona in personas:
     print(persona.edad)
    
answered by 28.11.2017 в 15:43