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

  • SEARCH
  • Home
  • 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 8914051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:38:03+00:00 2026-06-15T04:38:03+00:00

I’m using Flask-SQLAlchemy, and I’m trying to write a hybrid method in a parent

  • 0

I’m using Flask-SQLAlchemy, and I’m trying to write a hybrid method in a parent model that returns the number of children it has, so I can use it for filtering, sorting, etc. Here’s some stripped down code of what I’m trying:

# parent.py
from program.extensions import db
from sqlalchemy.ext.hybrid import hybrid_method

class Parent(db.Model):
    __tablename__ = 'parents'
    parent_id = db.Column(db.Integer, primary_key=True)

    name = db.Column(db.String(80))
    children = db.relationship('Child', backref='parent', lazy='dynamic')

    def __init__(self, name):
        self.name = name

    @hybrid_method
    def child_count(self):
        return self.children.count()

    @child_count.expression
    def child_count(cls):
        return ?????

# child.py
from program.extensions import db
from program.models import Parent

class Child(db.Model):
    __tablename__ = 'children'
    child_id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey(Parent.parent_id))

    name = db.Column(db.String(80))
    time = db.Column(db.DateTime)

    def __init__(self, name, time):
        self.name = name
        self.time = time

I’m running into two problems here. For one, I don’t know what exactly to return in the “child_count(cls)”, which has to be an SQL expression… I think it should be something like

return select([func.count('*'), from_obj=Child).where(Child.parent_id==cls.parent_id).label('Child count')

but I’m not sure. Another issue I have is that I can’t import the Child class from parent.py, so I couldn’t use that code anyway. Is there any way to use a string for this? For example,

select([func.count('*'), from_obj='children').where('children.parent_id==parents.parent_id').label('Child count')

Eventually, I’ll want to change the method to something like:

def child_count(cls, start_time, end_time):
    # return the number of children whose "date" parameter is between start_time and end_time

…but for now, I’m just trying to get this to work. Huge thanks to whoever can help me with this, as I’ve been trying to figure this out for a long time now.

  • 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-15T04:38:09+00:00Added an answer on June 15, 2026 at 4:38 am

    The code below shows it all.

    class Parent(Base):
        __tablename__ = 'parents'
        # ...
    
        @hybrid_property
        def child_count(self):
            #return len(self.children)   # @note: use when non-dynamic relationship
            return self.children.count()# @note: use when dynamic relationship
    
        @child_count.expression
        def child_count(cls):
            return (select([func.count(Child.child_id)]).
                    where(Child.parent_id == cls.parent_id).
                    label("child_count")
                    )
    
        @hybrid_method
        def child_count_ex(self, stime, etime):
            return len([_child for _child in self.children
                if stime <= _child.time <= etime ])
    
        @child_count_ex.expression
        def child_count_ex(cls, stime, etime):
            return (select([func.count(Child.child_id)]).
                    where(Child.parent_id == cls.parent_id).
                    where(Child.time >= stime).
                    where(Child.time <= etime).
                    label("child_count")
                    )
    
    
    # usage of expressions:
    stime, etime = datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 31)
    qry = session.query(Parent)
    #qry = qry.filter(Parent.child_count > 2)
    qry = qry.filter(Parent.child_count_ex(stime, etime) > 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.