Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide count() in Model #1721

Open
lnxsr opened this issue Sep 28, 2024 · 0 comments
Open

Provide count() in Model #1721

lnxsr opened this issue Sep 28, 2024 · 0 comments

Comments

@lnxsr
Copy link

lnxsr commented Sep 28, 2024

Is your feature request related to a problem? Please describe.
It took me quite some time as newbie to figure out how to get the count of all rows in a table.

Describe the solution you'd like
An easy way to get the count of rows of a table.

Provide count() in Model similar like this

@classmethod
async def count(cls):
    return (await cls.first().annotate(count=Count(<some column>)).count

Describe alternatives you've considered
The current workaround I ended up with is

class Counter:
    @classmethod
    async def count(cls):
        return (await cls.first().annotate(count=Count(next(iter(cls._meta.db_fields))))).count

class Tournament(Counter, Model):
    pass

Additional context
Maybe PK should be preferred if existing, with a fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant