Remove duplicates in a Django query

前端 未结 6 1187
北恋
北恋 2020-12-08 06:39

Is there a simple way to remove duplicates in the following basic query:

email_list = Emails.objects.order_by(\'email\')

I tried using

6条回答
  •  青春惊慌失措
    2020-12-08 07:14

    For checking duplicate you can do a GROUP_BY and HAVING in Django as below. We are using Django annotations here.

    from django.db.models import Count
    from app.models import Email
    
    duplicate_emails = Email.objects.values('email').annotate(email_count=Count('email')).filter(email_count__gt=1)
    

    Now looping through the above data and deleting all other emails except the first one (depends on requirement or whatever).

    for data in duplicates_emails:
        email = data['email']
        Email.objects.filter(email=email).order_by('pk')[1:].delete()
    

提交回复
热议问题