Last active
February 11, 2021 08:50
-
-
Save paschmaria/2a7b4870542b78cc5d7f85dc6525381b to your computer and use it in GitHub Desktop.
Deleting duplicate (multiple occurring) rows from database in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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