Monday, February 20, 2017

Re: Implementing a basic search for my website

You can split your query set in two. First search elements with both elements then elements with only one.

Obviously, that's two request, so there might be better way to handle the problem.

On 20 Feb 2017 4:33 p.m., "Carlo Ascani" <carlo.ratm@gmail.com> wrote:
Hi all,
I am just trying to add a search funcionality to my frontend.
I know it is a huge topic, so sorry my n00bih questions.

I have a model A

class A(models.Model):
    roles = models.ManyToManyField(B)
    location = models.ForeignKey(C)
    ...


These 2 fields are the only fields I care about
when searching, so I would say I need a very
specific field based search funcionality.

I wrote this view to handle it


class ASearch(ListView):
    model = A
    template_name = 'search_result.html'

    def get_context_data(self, **kwargs):
        context = super(A, self).get_context_data(**kwargs)
        results = A.objects.all()
        query = self.request.GET.get('q')

        if query:
            query_list = query.split()

            results = results.filter(
                reduce(operator.or_, (
                    Q(roles__name__icontains=q)
                        for q in query_list)) |
                reduce(operator.or_, (
                    Q(location__name__iexact=q)
                        for q in query_list))
                )

        context.update({
            'results': results
        })
        return context



This is quite working. I mean, results are fine.
But the problem is that I would like that searches
who covers *all* conditions to come first.

For example, let's say I search for 'MyRole MyLocation'
the results I get are all the As for MyRole and all the
As for MyLocation, mixed.

I would like that As with MyRole AND MyLocation to show
up first.


I hope I was clear enough, but I doubt it.

Thank you in advance

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2c34b391-95d3-4ae9-acb1-81649833a725%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEuG%2BTb_wx0YLZyx%2B7Ax1v7%2BsczrtZfCPYVFbcicQ2t7PhrK_w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment