You can download this code by clicking the button below.
This code is now available for download.
This function uses Django's ORM system to query the database for users that match specific conditions. It accepts three arguments: the first argument is the field value to match, the second argument is a list of field names, and the third argument is a string to search for within the username.
Technology Stack : Django, Django ORM, get_user_model, Q, filter, icontains
Code Type : Django model query
Code Difficulty : Intermediate
def random_choice_users(arg1, arg2, arg3):
from django.contrib.auth import get_user_model
from django.db.models import Q
User = get_user_model()
if arg1 is None:
raise ValueError("arg1 cannot be None")
if not isinstance(arg2, list):
raise TypeError("arg2 must be a list of user fields")
if not isinstance(arg3, str):
raise TypeError("arg3 must be a string representing the search query")
users = User.objects.filter(Q(**{field: arg1 for field in arg2}) & Q(username__icontains=arg3))
return users