[Answered ]-Filtering numbers from a django list

1👍

You can use a regular expression (regex) in your query to find strings with numbers:

users = User.objects.filter(id__regex=r'^[0-9]*$')

The '^[0-9]*$' will match only numbers from the beginning (^) of the string to the end ($) where the numbers [0-9] can be repeated (*). The r means that what is between the quotes is to be interpreted as a raw string, so the \ will be not be considered an escape character.

0👍

I tried this and it works, I don’t know if it is the best but it works!:

users = User.objects.all()
ids = [user.id for user in users if user.id.isnumeric()]

Leave a comment