Simple search in Django

后端 未结 4 1133
北海茫月
北海茫月 2021-01-30 17:36

I have a really simple blog application and I want to add a really simple search feature to it.

There are 3 key fields to my model.

class BlogPost(models         


        
4条回答
  •  忘了有多久
    2021-01-30 18:24

    If you want it to work just like the admin, you could try my mini-lib Django simple search. It's basically a port of the admin search functionality. Install it with pip:

    pip install django-simple-search
    

    and use it like:

    from simple_search import search_filter
    from .models import BlogPost
    
    search_fields = ['^title', 'intro', 'content']
    query = 'search term here'
    posts = BlogPost.objects.filter(search_filter(search_fields, query))
    

    I also wrote a blog post about it: https://gregbrown.co/projects/django-simple-search

提交回复
热议问题