Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:12:59+00:00 2026-06-09T14:12:59+00:00

I am learning pyramid and sqlalchemy and am struggling with how best to perform

  • 0

I am learning pyramid and sqlalchemy and am struggling with how best to perform a query in the database without nested foreach loops. I am sure there is a more efficient way.

I have the following models:

    class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        username = Column(Text)
        topicsfollowing = relationship('Topic', secondary=users_topics,
                                       backref='followers')

    class Article(Base):
        __tablename__ = 'articles'
        id = Column(Integer, primary_key=True)
        name = Column(Text)
        body = Column(Text)
        datepublished = Column(DateTime)
        focusid = Column(Integer, ForeignKey("topics.id"))
        focus = relationship("Topic", backref=backref('articles', order_by=id))

    class Topic(Base):
        __tablename__ = 'topics'
        id = Column(Integer, primary_key=True)
        name = Column(Text)

And I also set up the M2M relationship between users and topics as follows:

    users_topics = Table('users_topics', Base.metadata,
                  Column('userid', Integer, ForeignKey('users.id')),
                  Column('topicid', Integer, ForeignKey('topics.id')))

Essentially, my users can follow topics, and there are articles written about each of the topics (the focus). What I am trying to figure out is an efficient way to get a list of the 10 most recent articles written from the collection of all of the topics the user is following. For example, one particular topic could supply all 10 of the most recent, or there may be 10 topics, each supplying a single recent article.

The only way I can think to do it is something along the lines of:

    user = DBSession.query(User).filter_by(id=logged_in).first()
    for topic in user.topicsfollowing:
        for article in topic.articles:
            # from here assemble a list of all articles from all followed
            # topics and then sort them descending by datepublished and take 
            # the first 10 in the list

But I know there has to be a much more efficient way to do this. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:13:00+00:00Added an answer on June 9, 2026 at 2:13 pm

    You just need to do one query to get a list of the topic ids the user is following, then do something like:

    query(Article).filter(Article.focusid.in_(topic_ids)).order_by(desc(Article.datepublished)).limit(10)
    

    I’m writing that more or less off the top of my head, so I’m not sure if it’s 100% right, but see the “IN” operator in the docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning pyramid and it seems they are trying to get people to use
Learning a lot in my few years of programming that the best projects are
Learning java server technologies, trying to clarify some things. There are few technologies that
learning about loops (still a beginner) in VB.net. I have got the below code
Learning php! I have a sql database which contains a table called 'images' which
learning python, coming from a php background. Keeping it short. Is there a way
learning Jquery and integrating with PHP - getting there, but have one last challenge
I am still learning pyramid, and I am at a point where I am
I have trid learning the framework with the help of the book, The Pyramid
I'm learning pyramid. I'd like to see me some open source projects and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.