如何在django管理员中添加自定义搜索框 [英] how to add custom search box in django admin

查看:1253
本文介绍了如何在django管理员中添加自定义搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这将是一个非常基本的问题。

I know this is gonna be a very basic question.

在django中,我已经成功创建了一个管理面板。现在我想添加一个自定义搜索框我的领域,即照片 field.But我不知道如何添加自定义搜索框在django管理面板。如果我得到一些适当的提示比我相信我可以做到。

In django i have successfully created a admin panel.Now i want a 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)

我想在这个照片字段中添加一个自定义搜索框,其中可以通过 ID 搜索图像。现在如何我在上面给出的模型中添加了这个搜索框。

i want a 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.

推荐答案

使用 ModelAdmin 的 属性: $ $ onTrib.admin.ModelAdmin.search_fields>

Use the search_fields attribute of the ModelAdmin:

class PhotoAdmin(admin.ModelAdmin):
    ...
    search_fields = ('name', 'description', 'keyword', )

这篇关于如何在django管理员中添加自定义搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆