Skip to content

Instantly share code, notes, and snippets.

@brunomichetti
Created Sep 13, 2022
Embed
What would you like to do?
# Get the users that have a name but don't have a username
users = User.objects.filter(username__isnull=True, name__isnull=False)
# For each user apply the transform to get the username.
# This doesn't hit the database
for user in users:
user.username = user.name.replace(" ", "").lower()
# Hit the database
User.objects.bulk_update(users, ("username",))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment