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!
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With