In this blog I will be showing you how to add a custom filter to your django admin page in list_filter which allows you to see duplicate results.
Considering your models looks something like this
title = models.CharField(max_length=200, null=True, blank=True)
description = models.TextField(null=True, blank=True)
url = models.CharField(max_length=200, null=True, blank=True)
video_id = models.CharField(max_length=200, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Here, in this scenario we are assuming that somehow the we added the duplicate records now we want to filter out the records that have same video_id that means that those are duplicates.
NOTE: This video_id is different for the standard django primary key i.e id or pk. because by default django will never allow to add duplicates to those ids as they are primary key.
I have chosen this video_id as a field to detect which records have same ids that means those are same duplicates same could have been achieved using title but video_id made more sense and so, in your case the decision would matter thats why I am being kind of repetitive here.
For example: if you are doing this to profile model a good candidate can be your social media ids, phone numbers etc...
Now, coming to the implementation create a file
from django.contrib.admin import SimpleListFilter
This filter is being used in django admin panel.
title = 'Duplicates'
parameter_name = 'video_id'
def lookups(self, request, model_admin):
def queryset(self, request, queryset):
if not self.value():
if self.value().lower() == 'duplicates':
return queryset.filter().exclude(id__in=[video.id for video in queryset.distinct("video_id").order_by("video_id")])
and in you admin.py
from django.contrib import admin
from .models import *
from .custom_filter import DuplicatVideoFilter
list_filter = (DuplicatVideoFilter, )
This will filter out the duplicate records on the basis of video_id field.
THAT's IT!! ONLY IF EVERYTHING WAS THIS EASY..