djangosearchdjango-querysetoperationalerror

Django unrecognized token: "@" while query


I simply want to implement a search view into my Django app. But when i try to search something on my App i get the following error:

'unrecognized token: "@"'

In the end i want that my Query is a combination of category and searchword. So that the user can filter specific categories (Just like Amazon.com searchfield) e.g.: http://127.0.0.1:8000/search/?category=1&q=hallo

base.html

...
   <div class="globalsearch">
            <form id="searchform" action="{% url 'search' %}" method="get" accept-charset="utf-8">
                <label for="{{ categorysearch_form.category.id_for_label }}">In category: </label> {{ categorysearch_form.category }}
                <input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search for ...">
                <button class="searchbutton" type="submit">
                    <i class="fa fa-search"></i>
                </button>
            </form>
        </div>
    </div>

...

categorysearch_form is a dropdown selector that gets his ID from the Database.

views.py

...
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from django.views.generic import ListView


class globalsearch(ListView):
    """
    Display a Post List page filtered by the search query.
    """
    model = Post
    paginate_by = 10

    def get_queryset(self):
        qs = Post.objects.all()

        keywords = self.request.GET.get('q')
        if keywords:
            query = SearchQuery(keywords)
            title_vector = SearchVector('title', weight='A')
            content_vector = SearchVector('content', weight='B')
            tag_vector = SearchVector('tag', weight='C')
            vectors = title_vector + content_vector + tag_vector
            qs = qs.annotate(search=vectors).filter(search=query)
            qs = qs.annotate(rank=SearchRank(vectors, query)).order_by('-rank')

        return qs

...

urls.py

...

url(r'^search/$', views.globalsearch.as_view(), name='search'),

...

Search.html results are getting displayd here:

{% extends 'quickblog/base.html' %}

{% block content %}
    {% for post in object_list %}
        <div class="post">
            <h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
            <p>{{ post.content|linebreaksbr }}</p>
            <div class="date">
                <a>Published by: {{ post.author }}</a><br>
                <a>Published at: {{ post.published_date }}</a><br>
                <a>Category: {{ post.category }}</a><br>
                <a>Tag(s): {{ post.tag }}</a>
            </div>
        </div>
    {% endfor %}

Post Model

...
#Post Model
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=75)
    content = models.TextField(max_length=10000)
    tag = models.CharField(max_length=50, blank=True)
    category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
    postattachment = fields.FileField(upload_to='postattachment/%Y/%m/%d/', blank=True ,null=True)
    postcover = fields.ImageField(upload_to='postcover/%Y/%m/%d/', null=True, dependencies=[
        FileDependency(processor=ImageProcessor(
            format='JPEG', scale={'max_width': 200, 'max_height': 200}))
    ])
    created_date = models.DateField(auto_now_add=True)
    published_date = models.DateField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    class Meta:
        ordering = ["-title"]

    def __str__(self):
        return self.title
...

i guess im simply missing something at that point... I already read that this issue comes from local SQL database aka. SQLite, could that be true? Smb. has a solution? Thanks


Solution

  • After a bit of tinkering with a friend i found a solution, the Django Documentation according to vector searching is really not that good...

    views.py

    class globalsearch(ListView):
        """
        Display a Post List page filtered by the search query.
        """
        model = Post
        paginate_by = 10
    
        template_name = 'quickblog/search.html'
    
        def get_queryset(self):
            keywords = self.request.GET.get('q')
            if keywords:
                query = SearchQuery(keywords)
                title_vector = SearchVector('title', weight='A')
                content_vector = SearchVector('content', weight='B')
                tag_vector = SearchVector('tag', weight='C')
                vectors = title_vector + content_vector + tag_vector
                qs = Post.objects.annotate(rank=SearchRank(vectors, query)).filter(rank__gte=0.1).order_by('-rank')
                return qs
    

    models.py

    #Post Model
    class Post(models.Model):
        author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
        title = models.CharField(max_length=75)
        content = models.TextField(max_length=10000)
        tag = models.CharField(max_length=50, blank=True)
        category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
        postattachment = fields.FileField(upload_to='postattachment/%Y/%m/%d/', blank=True ,null=True)
        postcover = fields.ImageField(upload_to='postcover/%Y/%m/%d/', null=True, dependencies=[
            FileDependency(processor=ImageProcessor(
                format='JPEG', scale={'max_width': 200, 'max_height': 200}))
        ])
        created_date = models.DateField(auto_now_add=True)
        published_date = models.DateField(blank=True, null=True)
    
        def publish(self):
            self.published_date = timezone.now()
            self.save()
    
        class Meta:
            ordering = ["-title"]
    
        def __str__(self):
            return self.title
    

    search.html

    {% extends 'quickblog/base.html' %}
    
    {% block content %}
        {% for post in object_list %}
            <div class="post">
                <h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
                <p>{{ post.content|linebreaksbr }}</p>
                <div class="date">
                    <a>Published by: {{ post.author }}</a><br>
                    <a>Published at: {{ post.published_date }}</a><br>
                    <a>Category: {{ post.category }}</a><br>
                    <a>Tag(s): {{ post.tag }}</a>
                </div>
            </div>
        {% endfor %}
    
    
    {% if is_paginated %}
        <ul class="pagination">
            {% if page_obj.has_previous %}
                <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
            {% else %}
                <li class="disabled"><span>&laquo;</span></li>
            {% endif %}
            {% for i in paginator.page_range %}
                {% if page_obj.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                {% else %}
                    <li><a href="?page={{ i }}">{{ i }}</a></li>
                {% endif %}
            {% endfor %}
            {% if page_obj.has_next %}
                <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
            {% else %}
                <li class="disabled"><span>&raquo;</span></li>
            {% endif %}
        </ul>
    {% endif %}
    {% endblock %}
    

    urls.py

    url(r'^search/$', views.globalsearch.as_view(), name='search'),
    

    Hope that i was able to help the next one with that Post :D