Created
October 16, 2024 13:37
-
-
Save EnriqueSoria/5ddefc6cb592e2cf8f1a17744bd72dc5 to your computer and use it in GitHub Desktop.
Get duplicated instances 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
from typing import List | |
from django.core.exceptions import FieldError | |
from django.db.models import Count | |
from django.db.models import Q | |
from django.db.models import QuerySet | |
def get_duplicate_instances(queryset: QuerySet, fields: List[str]) -> List[QuerySet]: | |
""" | |
Get duplicate instances of a Django model based on specified fields. | |
This function queries the database to identify instances of the given Django model | |
that have duplicate values in the specified fields. It returns a list of QuerySets, | |
each containing the duplicate instances for a unique combination of field values. | |
""" | |
try: | |
values_with_duplicated_instances = ( | |
queryset.values(*fields).annotate(count=Count("id")).filter(count__gt=1).values(*fields) | |
) | |
except FieldError as err: | |
raise ValueError("One or more of the specified fields do not exist in the model.") from err | |
duplicated_instances = [] | |
for values in values_with_duplicated_instances: | |
filters = [Q(**{field_name: field_value}) for field_name, field_value in values.items()] | |
duplicated_instances.append(queryset.filter(*filters)) | |
return duplicated_instances |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment