Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Search_fields in Django

Tags:

I tried to add search fields in Django using python. Followings are the codes that I have used.

# admin.py file  from django.db import models from blog.models import Blog from django.contrib import admin admin.site.register(Blog)  class Blog(models.Model):     title = models.CharField(max_length=60)     body = models.TextField()     created = models.DateTimeField("Date Created")     updated = models.DateTimeField("Date Updated")     def __unicode__(self):         return self.title  class Comment(models.Model):     body = models.TextField()     author = models.CharField(max_length=60)     created = models.DateTimeField("Date Created")     updated = models.DateTimeField("Date Updated")     post = models.ForeignKey(Blog)     def __unicode__(self):         return self.body  class CommentInline(admin.TabularInline):     model = Comment   class BlogAdmin(admin.ModelAdmin):     list_display = ('title','created', 'updated')     search_fields = ['title','body']     list_filter = ('Date Created','Date Updated')     inlines = [CommentInline]  class CommentAdmin(admin.ModelAdmin):     list_display = ('post','author','body_first_60','created','updated')     list_filter = ('Date Created','Date Updated') 

I tried to add a search_fields for title and body by using Following code.

class BlogAdmin(admin.ModelAdmin):     . . .     search_fields = ('title','body')     . . . 

When I run this I can't see any search box. Why is that ? I want your help. I'm just a beginner. Thanks!

like image 811
Grant Avatar asked Jun 26 '12 16:06

Grant


People also ask

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

What is admin panel in Django?

Django provides a built-in admin module which can be used to perform CRUD operations on the models. It reads metadata from the model to provide a quick interface where the user can manage the content of the application. This is a built-in module and designed to perform admin related tasks to the user.


1 Answers

The search fields should be a list, not a tuple.

class BlogAdmin(admin.ModelAdmin):     . . .     search_fields = ['title','body']     . . .  

Then make sure that you associate this admin object with the model.

admin.site.register(Blog, BlogAdmin) 

EDIT:

It's hard to tell from above, but you should consider just importing the models from models.py instead of redefining them in your admin.py file. Again, it looks like that's what you're doing above.

admin.py:

from django.db import models from blog.models import Blog from django.contrib import admin  class CommentInline(admin.TabularInline):     model = Comment  class BlogAdmin(admin.ModelAdmin):     list_display = ('title','created','updated',)     search_fields = ['title','body',]     list_filter = ('Date Created','Date Updated',)     inlines = [CommentInline,]  class CommentAdmin(admin.ModelAdmin):     list_display = ('post','author','body_first_60','created','updated',)     list_filter = ('Date Created','Date Updated',)  admin.site.register(Blog, BlogAdmin) 

models.py

from django.db import models  class Blog(models.Model):     title = models.CharField(max_length=60)     body = models.TextField()     created = models.DateTimeField("Date Created")     updated = models.DateTimeField("Date Updated")      def __unicode__(self):         return self.title  class Comment(models.Model):     body = models.TextField()     author = models.CharField(max_length=60)     created = models.DateTimeField("Date Created")     updated = models.DateTimeField("Date Updated")     post = models.ForeignKey(Blog)      def __unicode__(self):         return self.body 
like image 96
Andrew Sledge Avatar answered Sep 20 '22 07:09

Andrew Sledge