Back

Pagination in python

  • 0
  • Databases
  • Functions
mitsy0_0
4 Jun, 2023, 22:12

Hey I want to retrieve all documents using my python function, for which I am using pagination

I am getting this error: {"error":"Expecting value: line 1 column 1 (char 0)"}

(Also could you refer me docs/ examples for pagination in python, I couldn't really find anything)

This is my function

TypeScript
def main(req, res):

  try:
    client = Client()

    if not req.variables.get('APPWRITE_FUNCTION_ENDPOINT') or not req.variables.get('APPWRITE_FUNCTION_API_KEY'):
      print('Environment variables are not set. Function cannot use Appwrite SDK.')
    else:
      (
      client
   .set_endpoint(req.variables.get('APPWRITE_FUNCTION_ENDPOINT', None))
        .set_project(req.variables.get('APPWRITE_FUNCTION_PROJECT_ID', None))
        .set_key(req.variables.get('APPWRITE_FUNCTION_API_KEY', None))
        .set_self_signed(True)
      )

      databases = Databases(client)

    def fetch_documents(cursor):
      all_docs = []
      query_options = [
          Query.orderDesc("age"),
          Query.limit(25),
      ]

      while True:
          response = databases.list_documents(
              '647ac763cf53ed9xxxxx',
              '647ac820ba38520xxxxx',
              query_options
          )
          try:
              if cursor:
                 last_id = response['documents'][-1]['$id']
                 query_options.append(Query.cursorAfter(last_id))
                 cursor = False

              documents = response['documents']
              all_docs.extend(documents)

              if len(documents) < 25:
                  break

              last_id = documents[-1]['$id']
              query_options[-1] = Query.cursorAfter(last_id)
          except Exception as e:
              all_docs = ["error"]
              print(e)
              break

      return all_docs
    
    return res.json({
      "result": fetch_documents(True),
    })
  
  except Exception as e:
      return res.json({
        "error": str(e)
      })```
TL;DR
The user is experiencing an issue with pagination in Python. They are getting an error regarding the 'Query' object and its attributes. They are importing the 'Query' object from the 'appwrite.query' module. They are also unsure of the correct functions and syntax for pagination in Python. Solution: - The user needs to make sure they are importing the correct 'Query' object and its attributes from the 'appwrite.query' module. - They can find related functions and syntax for pagination in Python from the Appwrite documentation: <https://appwrite.io/docs/pagination>. - The user should also check if they have set the
Drake
4 Jun, 2023, 23:54

The docs on pagination are here: https://appwrite.io/docs/pagination. The idea is the same, just change the syntax for python

Drake
4 Jun, 2023, 23:58

How about you test that fetch documents function locally?

Drake
5 Jun, 2023, 00:19

Also, did you create the endpoint variable? If so, what did you set the value to?

mitsy0_0
5 Jun, 2023, 06:45
mitsy0_0
5 Jun, 2023, 06:47

Yeah, actually that's what I tried😅

mitsy0_0
5 Jun, 2023, 06:52

Okay, so I was debugging my code line by line, and I get this error

{"error":"type object 'Query' has no attribute 'orderDesc'"}

this is how I imported query from appwrite.query import Query

What am I doing wrong?

mitsy0_0
5 Jun, 2023, 06:53
TypeScript
          Query.orderDesc("age"),
          Query.limit(25),
      ]


      response = databases.list_documents(
              '647ac763cf53edxxxxx',
              '647ac820ba38520xxxx',
              query_options
          )```
mitsy0_0
5 Jun, 2023, 07:22

um if I remove that query, I get type object 'Query' has no attribute 'cursorAfter'

Are the name of functions different in python? Where can I find python version of these functions😢

mitsy0_0
5 Jun, 2023, 08:42

Okay, I figured it out

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more