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()]
- [Answered ]-Django AuthenticationForm Error messages not showing
- [Answered ]-Making a smart class factory in Django
Source:stackexchange.com