D
D
Dennis2016-05-29 13:48:44
Django
Dennis, 2016-05-29 13:48:44

Django - implementation of object search?

Good afternoon!

models.py

    class Location(models.Model):
        user = models.ForeignKey(User)
        name = models.CharField(max_length=100, verbose_name=u"Локация", default=u'')
        keywords = models.CharField(max_length=100, verbose_name=u"Ключевые слова")

forms.py

    class AdvancedSearchForm(forms.Form):
        location = forms.CharField()
        keywords = forms.CharField() # e.g. 'spam,eggs,hum'

views.py

    class AdvancedSearchView(FormView):
        form_class = AdvancedSearchForm
        template_name = "advanced_search.html"
        success_url = '/search_location/result/'
     
    # url of this view is 'search_result'
    class SearchResultView(ListView):
        model = Location
        context_object_name = 'locations_searched'
        paginated_by = 5
        template_name = "search_result.html"
     
        def get_queryset(self):
            queryset = super(SearchResultView, self).get_queryset()
            location = self.request.GET.get('location')
            location = location.upper()
            keywords = self.request.GET.get('keywords')
            return queryset.filter(Q(name=location)|Q(keywords=keywords))

advanced_search.html

    <form action="{% url 'search_result' %}" method="GET">{% csrf_token %}
           {{ form|crispy }}
           <button class="btn btn-default" type="submit">Find</button>
    </form>

search_result.html

    {% for location in locations_searched %}
           {{ location }}<br>
           {{ location.user }}<br>
           {{ location.keywords }}<br>
    {% endfor %}

The idea is this - when creating an object, the keywords are stored in the database as a string of words separated by commas.
When searching for objects, I also fill in the keywords form field with words separated by commas.
The question is how to implement the search for the corresponding objects using such an algorithm - each word entered into the form must be searched for in each object (in the keywords field of the model) and if at least one word matches, the object must be returned.
PS As you can see, this implementation of Q(keywords=keywords) is looking for a comparison, for example, "spam, boor" with "eggs, boor, spam". And it is necessary that it would take "spam" and find it in "eggs, boor, spam" and return this object.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mr_Floppy, 2016-05-29
@Mr_Floppy

More or less like this:

filters = Q()

for word in keywords.split(','):
                filters |= Q(keywords__icontains=word)

Item.objects.filter(filters)

S
sim3x, 2016-05-29
@sim3x

models.py

    class Location(models.Model):
        user = models.ForeignKey(User)
        name = models.CharField(max_length=100)
        keywords = models.ManyToMany(Keyword)

    class Keyword(models.Model):
        name = models.CharField(max_length=100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question