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

Commit

Permalink
feat: Custom filter for admin to locate duplicate records
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed Apr 26, 2024
1 parent 752b8f8 commit 3b718c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postcards/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.html import format_html
from import_export.admin import ExportMixin

from .filters import DuplicateFilter
from .models import (
Censor,
Image,
Expand Down Expand Up @@ -159,6 +160,7 @@ class ObjectAdmin(ExportMixin, admin.ModelAdmin):
"date_of_correspondence",
"regime_censor",
"regime_location",
DuplicateFilter,
)
filter_horizontal = ("postmark",)
extra = 1
Expand Down
22 changes: 22 additions & 0 deletions postcards/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import django_filters
from dateutil.parser import parse
from django.contrib.admin import SimpleListFilter
from django.db.models import Q
from django.shortcuts import render

Expand Down Expand Up @@ -51,6 +52,27 @@ def __init__(self, *args, **kwargs):
).distinct()


class DuplicateFilter(SimpleListFilter):
"""this filter is used inside of django-admin to find duplicate files"""

title = "Duplicates"
parameter_name = "item_id"

def lookups(self, request, model_admin):
return (("true", "Duplicates"),)

def queryset(self, request, queryset):
if self.value():
return queryset
if self.value() is not None:
if self.value().lower() == "duplicates":
return queryset.filter().exclude(
id__in=[
i.id for i in queryset.distinct("item_id").order_by("item_id")
]
)


class ObjectFilter(django_filters.FilterSet):
class Meta:
model = Object
Expand Down

0 comments on commit 3b718c9

Please sign in to comment.