Python 如何在 Django-admin 中添加自定义搜索框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28512710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add custom search box in Django-admin?
提问by Md. Tanvir Raihan
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 boxin a django-admin panel. If I get some proper hints than I believe that I can do it.
在 Django 中,我已经成功创建了一个管理面板。现在我想在我的字段之一即Photo
字段中添加一个自定义搜索框。但我不知道如何在 django-admin 面板中添加自定义搜索框。如果我得到一些适当的提示,我相信我可以做到。
Admin.py:
管理.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:
模型.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.
我想在这个Photo
字段中添加一个自定义搜索框,可以通过它搜索图像ID
。现在我如何在上面给定的模型中添加这个搜索框。
采纳答案by catavaran
Use the search_fields
attribute of the ModelAdmin
:
使用 的search_fields
属性ModelAdmin
:
class PhotoAdmin(admin.ModelAdmin):
...
search_fields = ('name', 'description', 'keyword', )
回答by Dan Walters
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]) # It take a model or an iteratble