Good day!

Please tell me where you can see the implementation of this type of search form, for example:

class Place(models.Model): name = models.CharField() keywords = models.CharField() 

There is a view with a form for creating a Place object, while keywords are a string of comma separated words.

The task is to create a view with a form for searching objects by name and keywords, which will also be entered as comma separated words.

Thank!

    1 answer 1

    If you want to search by two model fields in one form field, you can use a Q object with the operator | which means or
    views.py:

     from django.views.generic import ListView from django.db.models import Q class PlaceListView(ListView): model = Place def get_queryset(self): # Получаем не отфильтрованный кверисет всех моделей queryset = super(FlavorListView, self).get_queryset() q = self.request.GET.get("q") if q: # Если 'q' в GET запросе, фильтруем кверисет по данным из 'q' return queryset.filter(Q(name__icontains=q)| Q(keywords__icontains=q)) return queryset 

    urls.py:

     urlpatterns = patterns( '', url(r'^place_search/$', PlaceListView.as_view, name='place_search'),) 

    Form in the template:

     <form action="{% url "place_search" %}" method="GET"> <input type="text" name="q" /> <button type="submit">search</button> </form> <!--> Сюда можно включить шаблон с результатами через include Или выделить для него отдельный блок <--> {% include "template/result_template.html" %} 

    Template with search form results:

     <h1>Places</h1> <ul> {% for place in object_list %} <li>{{ place.name }}</li> {% endfor %} </ul> 

    UPD
    If you want to search for the occurrence of each word, separated by commas, you can break a line into a list and look for an entry in it.

     .... q = self.request.GET.get("q") q_lst = q.split(",") return queryset.filter(Q(name__in=q_lst)| Q(keywords__in=q_lst)) 
    • see what the situation is - when creating an object in the keywords field, a line will go (for example, “spam, eggs, ham”) .. I want to receive this object when searching, even if only “spam” or “garbage, spam” and etc. .. maybe I'm wrong, but according to your code it seems to me that there will be an incorrect comparison (through contains) .. i.e. The code will check that "garbage, spam" is in "spam, eggs, ham" ... or am I wrong? - Dennis
    • @Dennis yes that's right, i__contains looking for a complete word entry, you can also add a search for individual words, after making a list of them. Update answer now - Ivan Semochkin
    • Thank you Ivan! I understood the direction :) but still there is a question - it turns out that Q (keywords__in = q_lst) will look in my opinion this way - for example, if the user specified the field "spam, eggs" when creating an object, then when searching, when entering in the form of "spam, eggs, some, any" will start the search for the type "spam, eggs" in ['spam', 'eggs', 'some', 'any'] ... I also need to match every word in keywords every word in the form .. i.e. betrayed an object even if one word matches - Dennis