Skip to content

Instantly share code, notes, and snippets.

@brunomichetti
Created September 13, 2022 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomichetti/41625ce7dd2f481dacc5bf771563c130 to your computer and use it in GitHub Desktop.
Save brunomichetti/41625ce7dd2f481dacc5bf771563c130 to your computer and use it in GitHub Desktop.
# 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