如何在Django-admin中添加自定义搜索框? [英] How to add custom search box in Django-admin?

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

问题描述

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

I know this is gonna be a very basic question.

在Django中,我已经成功创建了一个管理面板。现在,我想在我的一个字段即 Photo 字段中添加一个自定义搜索框。但我不知道django-admin面板中的如何添加自定义搜索框。如果我得到一些暗示的提示,但我相信我可以做到。

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)

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

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.

推荐答案

使用 <$ c ModelAdmin 的$ c> search_fields 属性:

Use the search_fields attribute of the ModelAdmin:

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

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

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