Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
refactor: queryset filtering for duplicate files in django-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed Apr 26, 2024
1 parent 7ee032f commit 453938d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions postcards/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import django_filters
from dateutil.parser import parse
from django.contrib.admin import SimpleListFilter
from django.db.models import Q
from django.db.models import Count, Q
from django.shortcuts import render

from postcards.models import Collection, Location, Object, PrimarySource
Expand Down Expand Up @@ -62,14 +62,14 @@ def lookups(self, request, model_admin):
return (("true", "Duplicates"),)

def queryset(self, request, queryset):
if self.value():
if self.value().lower() == "duplicates":
# Annotate each object in the queryset with a count of how many objects have the same item_id
queryset = queryset.annotate(item_id_count=Count("item_id"))

# Filter the queryset to include only objects where item_id_count is greater than 1
return queryset.filter(item_id_count__gt=1)
return queryset
"""find the duplicates to display"""
if self.value() == "true":
return queryset.filter(
item_id__in=Object.objects.values("item_id")
.annotate(item_count=Count("item_id"))
.filter(item_count__gt=1)
.values_list("item_id", flat=True)
)


class ObjectFilter(django_filters.FilterSet):
Expand Down

0 comments on commit 453938d

Please sign in to comment.