Skip to content

Instantly share code, notes, and snippets.

@paschmaria
Last active February 11, 2021 08:50
Show Gist options
  • Save paschmaria/2a7b4870542b78cc5d7f85dc6525381b to your computer and use it in GitHub Desktop.
Save paschmaria/2a7b4870542b78cc5d7f85dc6525381b to your computer and use it in GitHub Desktop.
Deleting duplicate (multiple occurring) rows from database in Django
# Get all user wallet IDs
wallets = User.objects.all().values_list('wallet', flat=True)
# check total number of wallet ids
wallets.count()
# get unique wallet IDs
unique_wallets = set(list(wallets))
# get total number of unique wallets
len(unique_wallets)
# get duplicate wallet IDs
for item in wallets:
if list(wallets).count(item) > 2:
multi.append(item)
# get number of duplicate wallet IDs
len(multi)
# delete object with duplicate IDs
for item in multi:
dup_users = User.objects.filter(wallet=item)[1:]
for user in dup_users:
user.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment