简体   繁体   中英

How can I delete duplicated users in django?

I need to delete duplicate users in django (by duplicate I mean two or more users with the same email).

If for instance there are three records like this:

id    email
3     c@c.com
56    c@c.com
90    c@c.com

I need to delete records 56 and 90 and leave the oldest record id -> 3

Is there a way to quickly do this.

Thanks :)

RZs answer is actually nearly correct. I don't know if it's the best way, but it works. So for this one time purpose you can use it.

However, I would like to add and correct some stuff.

from django.contrib.auth.models import User

def delete_duplicate_users():
  // first find all email addresses (with kind of a 'group by')
  emails = User.objects.values('email').distinct()

  for e in emails:
    users = User.objects.filter(email=e['email']).order_by('date_joined')[1:]
    for u in users:
      u.delete()

I tried this with a small example and it worked. But I highly recommend that you test this before you actually use it on your production system!

Hope that helps you.

// Edit

I also would recommend that you don't allow adding users if the email is already registered. There should be some built in method to achieve this. And if not you could subclass the Djangos User model with you own User model and override the save method.

users = User.objects.filter(email='c@c.com').order_by('join_date')[1:]
for u in users:
    u.delete()

I forget if querysets supports slicing like above and can't test right now. If they don't you just need to extract the first element and delete the rest.

You can get the email addresses like this.

from django.contrib.auth.models import User
from django.db.models import Count

duplicate_emails = [i['email'] for i in User.objects.values('email').annotate(
    Count('email')).filter(email__count__gt=1)]

Then you can loop through the email addresses and decide what to do with them. This example deletes the user with an older last_login date.

for email in duplicate_emails:
    user = User.objects.filter(email=email).order_by('last_login')[0]
    user.delete()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM