django prevent duplicate entries 1

django prevent duplicate entries

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)

Here is what the above code is Doing:
1. We’re using the values() method to return a list of dictionaries containing the email field and the number of times it appears in the database.
2. We’re using the annotate() method to add a new field to each dictionary called email_count.
3. We’re using the filter() method to return only the dictionaries where the email_count is greater than 1.

Similar Posts