Hi everyone,
I am trying to write a function in Python that retrieves data for all items in a given Directus collection. Here is the current function:
def get_collection(directus_key, collection_name, sort_by = 'date_created',fields = None):
DIRECTUS_API_URL = "https://cms.prod.mywebsite.net/items/" + collection_name
file_id = ''
all_files = []
limit = 100 # Maximum number of files per page
page = 1 # Start with the first page
headers = {
"Authorization": f"Bearer {directus_key}"
}
try:
max_progress = 100
pbar = tqdm(total=max_progress, desc="Processing", position=0, leave=True)
cursor = None
if fields != None:
fields += ', ' + sort_by
while True:
if fields == None:
params={"limit": limit, 'sort': sort_by}
else:
#limit = 20
params={"fields": fields, "limit": limit, 'sort': sort_by}
#deep_field = fields.split('.')[0]
#params[f"deep[{deep_field}][_limit]"] = 400
if cursor:
params["filter[" + sort_by + "][_gt]"] = cursor
response = requests.get(DIRECTUS_API_URL, headers=headers, params=params)
time.sleep(2)
if response.status_code == 200:
data = response.json()["data"]
if not data: # If no more files, stop the loop
break
cursor = data[-1][sort_by]
if fields != None:
for element in data:
del element[sort_by]
all_files.extend(data)
page += 1 # Move to the next page
else:
print(f"Failed to fetch files: {response.status_code} {response.text}")
break
pbar.update(1)
if page > max_progress:
max_progress += 100
pbar.total = max_progress
except Exception as e:
print("An error occurred:", str(e))
print('exiting program')
exit(0)
return all_files
Right now, it sorts the items by “date_created”. The issue is that sometimes, there are multiple items that are created simultaneously, which causes bugs in this function’s sorting and filtering (sorting by “date_updated“ runs in to similar issues).
I would therefore like to sort by the item’s id, since this is unique. The issue is, when I try passing ‘id‘ into the sort_by parameter, I get the following error:
"errors":[{"message":"Invalid query. \"uuid\" field type does not contain the \"_gt\" filter operator.","extensions":{"reason":"\"uuid\" field type does not contain the \"_gt\" filter operator","code":"INVALID_QUERY"}}]
Since “id” is not numeric, it cannot use the _gt filter. Is there anyway around this?