[Answer]-Django: How to get a model class field from other model class?

1👍

You just have to follow the foreign key field like this:

class Pedido(models.Model):

    idcliente = models.ForeignKey(Cliente, db_column='idCliente')

    def __unicode__(self):
        return self.idcliente.nome, self.id

The Django ORM will automatically perform a database lookup if required to required to load the appropriate Cliente row.

This will get the nome and the id.

Leave a comment