librelist archives

« back to archive

show the name of friends

show the name of friends

From:
peg kh
Date:
2015-01-01 @ 17:56
Happ new Year!! I have a app that you can make friend. I am able to count
the number of friends But Not able to show the name of those So Need Help
Please

model.py

from app import db

friends = db.Table('friends',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('friend_id', db.Integer, db.ForeignKey('user.id'))
)

class User(db.Model):
   id = db.Column(db.Integer, primary_key = True)
   name = db.Column(db.String(50), index=True, unique= True)
   email = db.Column(db.String(50),index=True, unique= True)


   is_friend = db.relationship('User', #defining the relationship, User is
left side entity
        secondary = friends, #indecates association table
        primaryjoin = (friends.c.user_id == id), #condition linking the
left side entity
        secondaryjoin = (friends.c.friend_id == id),#cond if link right.s
ent. with assoc table
        backref = db.backref('friends', lazy = 'dynamic'),#how accessed
from right
        lazy = 'dynamic'
    )

view.py

@app.route('/profile/<int:id>')
def profile(id):
  user= User.query.get(id)
  return render_template('profile.html', id=id, user= user)

and profile.html

{% extends "base.html" %}

  {% block content %}
     <h2>show users</h2>
          <strong>name:</strong> {{user.name}} <br>
           <strong>email:</strong> {{user.email }} <br>
           <strong>age:</strong> {{user.age}} <br>
           <br>
           <p>{{ user.is_friend.count() }} friends <p>
            # Not showing friends Name
           <p>{{ user.is_friend.name }} is friend</p>

  {% endblock %}

Re: [flask] show the name of friends

From:
Joshua J. Kugler
Date:
2015-01-01 @ 21:50
The Flask Mega Tutorial does something similar to this (followers).  You might 
be able to get some tips from it here: http://goo.gl/a71y07

j

On Thursday, January 01, 2015 19:56:21 peg kh wrote:
> Happ new Year!! I have a app that you can make friend. I am able to count
> the number of friends But Not able to show the name of those So Need Help
> Please
> 
> model.py
> 
> from app import db
> 
> friends = db.Table('friends',
> db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
> db.Column('friend_id', db.Integer, db.ForeignKey('user.id'))
> )
> 
> class User(db.Model):
>    id = db.Column(db.Integer, primary_key = True)
>    name = db.Column(db.String(50), index=True, unique= True)
>    email = db.Column(db.String(50),index=True, unique= True)
> 
> 
>    is_friend = db.relationship('User', #defining the relationship, User is
> left side entity
>         secondary = friends, #indecates association table
>         primaryjoin = (friends.c.user_id == id), #condition linking the
> left side entity
>         secondaryjoin = (friends.c.friend_id == id),#cond if link right.s
> ent. with assoc table
>         backref = db.backref('friends', lazy = 'dynamic'),#how accessed
> from right
>         lazy = 'dynamic'
>     )
> 
> view.py
> 
> @app.route('/profile/<int:id>')
> def profile(id):
>   user= User.query.get(id)
>   return render_template('profile.html', id=id, user= user)
> 
> and profile.html
> 
> {% extends "base.html" %}
> 
>   {% block content %}
>      <h2>show users</h2>
>           <strong>name:</strong> {{user.name}} <br>
>            <strong>email:</strong> {{user.email }} <br>
>            <strong>age:</strong> {{user.age}} <br>
>            <br>
>            <p>{{ user.is_friend.count() }} friends <p>
>             # Not showing friends Name
>            <p>{{ user.is_friend.name }} is friend</p>
> 
>   {% endblock %}

-- 
Joshua J. Kugler - Fairbanks, Alaska
Azariah Enterprises - Programming and Website Design
joshua@azariah.com - Jabber: pedahzur@gmail.com
PGP Key: http://pgp.mit.edu/  ID 0x73B13B6A