Skip to content

Instantly share code, notes, and snippets.

@jmorenoamor
Created July 26, 2017 08:36
Show Gist options
  • Save jmorenoamor/4ee70646b809e75516ba557286807f56 to your computer and use it in GitHub Desktop.
Save jmorenoamor/4ee70646b809e75516ba557286807f56 to your computer and use it in GitHub Desktop.
Find duplicate models using Django ORM
from apps.projects.models import Project
from django.db.models import Count
# Find duplicated models based on the value of one or more fields
dupes = Project.objects.values('name').annotate(Count('id')).order_by().filter(id__count__gt=1)
dupes = Project.objects.values('name', 'code').annotate(Count('id')).order_by().filter(id__count__gt=1)
dupes = Project.objects.values('name', 'code', 'release').annotate(Count('id')).order_by().filter(id__count__gt=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment