Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

SQLAlchemy ordering by count on a many to many relationship

likes = db.Table('likes',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20))

    def __repr__(self):
        return "<User('%s')>" % self.username

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255))

    likes = db.relationship('User', secondary = likes,
        backref = db.backref('posts', lazy='dynamic'))

    def __repr__(self):
        return "<Post('%s')>" % self.title
Comment

SQLAlchemy ordering by count on a many to many relationship

db.session.query(Post, func.count(likes.c.user_id).label('total')).join(likes).group_by(Post).order_by('total DESC')
Comment

PREVIOUS NEXT
Code Example
Python :: docstring python pycharm 
Python :: Not getting values from Select Fields with jQuery 
Python :: How deploy Flask application on Webfaction 
Python :: singke line expresions python 
Python :: jsfakjfkjadjfksajfa 
Python :: Python (cpython 2.7.16) sample 
Python :: Retry function for sending data 
Python :: python json string indices must be integersAdd Answer 
Python :: Best websites to learn Python 
Python :: how to close python in terminal 
Python :: ring Conversion Number 
Python :: swagger django 
Python :: list duplicates of specific file in folder python 
Python :: ring create an application to ask the user about his/her name. 
Python :: how to write stuff in python 
Python :: how to create dataframe from rdd 
Python :: discord rich presence python 
Python :: sympy.diff 
Python :: IPython default setup 
Python :: how to threshold filter geodataframe by column value 
Python :: dataflair python 
Python :: Obtener el valor ASCII de un carácter en Python 
Python :: python for skip header line 
Python :: get list of all document in django-elasticsearch-dsl 
Python :: 1038 solution python 
Python :: how make aloop in python 
Python :: 2 liste to a dictionary 
Python :: using the return statement, defining a function, with input from the user. 
Python :: how to fetch reverse foreign key on model object django 
Python :: python copy list from index 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =