Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize/rework the build_user_list helper function #69

Open
jjnesbitt opened this issue Aug 23, 2021 · 0 comments
Open

Optimize/rework the build_user_list helper function #69

jjnesbitt opened this issue Aug 23, 2021 · 0 comments

Comments

@jjnesbitt
Copy link
Member

def build_user_list(validated_data: OrderedDict) -> list:
"""
Build a list of user objects from an ordered dictionary of user data.
Accepts an ordered dictionary containing a list of validated user data, e.g.
as a result of validating a PermissionsSerializer with request data.
Returns a list of user objects.
"""
user_list = []
for valid_user in validated_data:
user_object = get_object_or_404(User, username=valid_user['username'])
user_list.append(user_object)
return user_list

This function is used to create user objects from usernames, but does so by fetching user objects from the database in a loop. This is inefficient, and should be done in bulk. The main question is how username that are not found should be handled and errors returned to the endpoint. One idea I had was to do a filter on User objects to return the full users, and for any usernames not in the returned list, we can raise an error stating that they do not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant