How to add custom search box in Django-admin?
Question:
I know this is gonna be a very basic question.
In Django, I have successfully created an admin panel. Now I want to add a custom search box in one of my field namely Photo
field. But I don’t know how to add custom search box in a django-admin panel. If I get some proper hints than I believe that I can do it.
Admin.py:
from django.contrib import admin
from photo.models import Photo,
class PhotoAdmin(admin.ModelAdmin):
list_display=('name','approved','approved_time','uploaded_time','user')
models.py:
class Photo(models.Model):
name = models.CharField(max_length = 100)
photo = models.ImageField(upload_to = 'photos', blank=False,null=True)
approved = models.BooleanField(default = False)
approved_time = models.DateTimeField(auto_now=True,null=True,blank=True)
uploaded_time = models.DateTimeField()
description = models.CharField(max_length = 500 , blank = False , null = True)
keyword = models.CharField(max_length = 500 , blank = False , null = True)
image_id = models.CharField(max_length=300, blank=True, null=True)
Certified = models.BooleanField(default = False)
approved_by = models.CharField(max_length = 100)
user = models.ForeignKey(User)
total_download = models.IntegerField(default=0)
watermarked_image = models.ImageField(upload_to = 'temp', blank=True,null=True)
I want to add a custom search box in this Photo
field where image can be searched by it’s ID
.Now how can I add this search box in my above given model.
Answers:
Use the search_fields
attribute of the ModelAdmin
:
class PhotoAdmin(admin.ModelAdmin):
...
search_fields = ['name', 'description', 'user__related_fieldname','keyword', ]
cant reply due to low karma..
but don’t forget to register the Admin Model too, like
admin.py
from django.contrib import admin
from .models import *
admin.site.register(Photo, PhotoAdmin)
When you write:
admin.site.register([Photo, PhotoAdmin])
you register in admin two models: Photo and PhotoAdmin, you must register Model and ModelAdmin for it, like this:
admin.site.register(Photo, PhotoAdmin)
Adding Dan Walters answer, you can also search if you don’t know the exact word with __icontains
class PhotoAdmin(admin.ModelAdmin):
...
search_fields = ['name__icontains', 'description', 'user__related_fieldname','keyword__icontains',
]
it’s equivalent to LIKE
in SQL
I know this is gonna be a very basic question.
In Django, I have successfully created an admin panel. Now I want to add a custom search box in one of my field namely Photo
field. But I don’t know how to add custom search box in a django-admin panel. If I get some proper hints than I believe that I can do it.
Admin.py:
from django.contrib import admin
from photo.models import Photo,
class PhotoAdmin(admin.ModelAdmin):
list_display=('name','approved','approved_time','uploaded_time','user')
models.py:
class Photo(models.Model):
name = models.CharField(max_length = 100)
photo = models.ImageField(upload_to = 'photos', blank=False,null=True)
approved = models.BooleanField(default = False)
approved_time = models.DateTimeField(auto_now=True,null=True,blank=True)
uploaded_time = models.DateTimeField()
description = models.CharField(max_length = 500 , blank = False , null = True)
keyword = models.CharField(max_length = 500 , blank = False , null = True)
image_id = models.CharField(max_length=300, blank=True, null=True)
Certified = models.BooleanField(default = False)
approved_by = models.CharField(max_length = 100)
user = models.ForeignKey(User)
total_download = models.IntegerField(default=0)
watermarked_image = models.ImageField(upload_to = 'temp', blank=True,null=True)
I want to add a custom search box in this Photo
field where image can be searched by it’s ID
.Now how can I add this search box in my above given model.
Use the search_fields
attribute of the ModelAdmin
:
class PhotoAdmin(admin.ModelAdmin):
...
search_fields = ['name', 'description', 'user__related_fieldname','keyword', ]
cant reply due to low karma..
but don’t forget to register the Admin Model too, like
admin.py
from django.contrib import admin
from .models import *
admin.site.register(Photo, PhotoAdmin)
When you write:
admin.site.register([Photo, PhotoAdmin])
you register in admin two models: Photo and PhotoAdmin, you must register Model and ModelAdmin for it, like this:
admin.site.register(Photo, PhotoAdmin)
Adding Dan Walters answer, you can also search if you don’t know the exact word with __icontains
class PhotoAdmin(admin.ModelAdmin):
...
search_fields = ['name__icontains', 'description', 'user__related_fieldname','keyword__icontains',
]
it’s equivalent to LIKE
in SQL