On Sat, Mar 9, 2019 at 1:58 PM C <joonasn...@hotmail.fr> wrote: > > Hello every one, > > I am new on Python/SqlAlchemy and I try to develop an app but I can't find > how to display the name of people and not their foreignkey when I reference > them, could you help me ? > > Here are my classes : > > > > from application.main import db > > > class Lettre(db.Model): > __tablename__ = "lettre" > lettre_id = db.Column(db.Integer, unique=True, nullable=False, > primary_key=True) > titre = db.Column(db.Text, nullable=False) > contenu = db.Column(db.Text, nullable=False) > date_label = db.Column(db.Text, nullable=False) > date_norm = db.Column(db.Text, nullable=False) > lettre_expediteur = db.Column(db.Text, > db.ForeignKey('correspondant.nom'), autoincrement='ignore_fk') > lettre_destinataire=db.Column(db.Text, > db.ForeignKey('correspondant.nom'), autoincrement='ignore_fk') > depuis_lieu=db.Column(db.Text, db.ForeignKey('lieu.lieu_id')) > vers_lieu=db.Column(db.Text, db.ForeignKey('lieu.lieu_id')) > > > > # Nous avons ici créé une première classe (table) pour notre base de données. > class Correspondant(db.Model): > __tablename__ = "correspondant" > id_correspondant = db.Column(db.Integer, unique=True, nullable=False, > primary_key=True) > nom = db.Column(db.Text, nullable=False) > prenom = db.Column(db.Text, nullable=False) > > > > class Lieu(db.Model): > __tablename__ = "lieu" > lieu_id=db.Column(db.Integer, unique=True, nullable=False, > primary_key=True) > label=db.Column(db.Integer, unique=True, nullable=False) > > > > > > > And here is what I wrote in my HTML pages : > > > <br/> > <h1>{{correspondance.titre}}</h1> > <dl> > <dt>Expéditeur</dt> > <dd>{{correspondance.lettre_expediteur}}</dd> > <dt>Date</dt> <dd>{{correspondance.date_label}}</dd> > <dt>Destinataire</dt> > <dd>{{correspondance.lettre_destinataire}}</dd> > <dt>Contenu</dt> <dd>{{correspondance.contenu}}</dd> > </dl> > > > But I always get the foreign key and not the name (nom) of the correspondant. > Could you help me please ?
You need to create a "relationship" between your 2 classes. This is separate from the foreign key (although they often use your foreign key definitions as the basis for the relationship). https://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship Hope that helps, Simon -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.