Django

Django Admin filter duplicate values

Django Admin list_filter (1).png

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

class Video(models.Model):
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)

def __str__(self):
return self.title

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

custom_filters.py

from django.contrib.admin import SimpleListFilter

class DuplicatVideoFilter(SimpleListFilter):
"""
This filter is being used in django admin panel.
"""
title = 'Duplicates'
parameter_name = 'video_id'

def lookups(self, request, model_admin):
return (
('duplicates', 'Duplicates'),
)

def queryset(self, request, queryset):
if not self.value():
return queryset
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

class VideoAdmin(admin.ModelAdmin):
list_filter = (DuplicatVideoFilter, )

admin.site.register(Video, VideoAdmin)

This will filter out the duplicate records on the basis of video_id field.

Screen Shot 2020-05-03 at 12.03.58 AM.png

THAT's IT!! ONLY IF EVERYTHING WAS THIS EASY..



About author

shahraiz ali.jpeg

Shahraiz Ali

I'm a passionate software developer and researcher from Pakistan. I like to write about Python, Django and Web Development in general.



Scroll to Top