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

count field in restapi #690

Closed
vincentchevrier opened this issue Jul 6, 2022 · 1 comment
Closed

count field in restapi #690

vincentchevrier opened this issue Jul 6, 2022 · 1 comment

Comments

@vincentchevrier
Copy link

Using the py4web documentation example of pydal.restapi.

The following url
http://127.0.0.1:8000/superheroes/api/tag?@limit=1&@count=True
will give the following response

{
  "api_version": "0.1",
  "code": 200,
  "count": 10,
  "items": [
    {
      "id": 1,
      "strength": 100,
      "superhero": 1,
      "superpower": 1
    }
  ],
  "status": "success",
  "timestamp": "2022-07-06T21:09:10.940419"
}

note that the count is incorrect, as it should be 1.
I believe this is because the rows are filtered by limit after applying the query here:

pydal/pydal/restapi.py

Lines 464 to 466 in daa85d4

rows = db(query).select(
*tfields, limitby=(offset, limit + offset), orderby=orderby
)

but count is obtained using only the query which does not apply the limit

response["count"] = db(query).count()

Presumably count could be obtained by using the length of rows instead?

@mdipierro
Copy link
Contributor

It is not incorrect. count is not supposed to return the number of items in the current response but the total number number of items matching the query. So you know if you need to query for more or not.

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

2 participants