Search code examples
djangodjango-modelsdjango-viewsdjango-templates

Django - Search Query Results Loading Incorrectly


On my search results page for some reason when a logged in user makes a search query in my gaming application all the results display instead of the query itself.

What needs to get fixed with what I currently have?

I’m building a gaming application where logged in users have access to play different games based on their rank in our application… so when a user makes a search for lets say the letter d all the unlocked games and locked games that have the letter d should display. I'm currently getting that but the search query shows all results that don't inlcude letter d.

This is only happening when a user is logged in. When a user is logged out the search works correctly, a search query for d will show results of every game with the character d in it.

Any help is gladly appreciated!

Thanks!

Below is my code.

models.py

class Game_Info(models.Model):
    id = models.IntegerField(primary_key=True, unique=True, blank=True, editable=False)
    game_title = models.CharField(max_length=100, null=True)
    game_rank = models.IntegerField(default=1)
    game_image = models.ImageField(default='default.png', upload_to='game_covers', null=True, blank=True)

class User_Info(models.Model):
    id = models.IntegerField(primary_key=True, blank=True)
    image = models.ImageField(default='/profile_pics/default.png', upload_to='profile_pics', null=True, blank=True)
    user = models.OneToOneField(settings.AUTH_USER_MODEL,blank=True, null=True, on_delete=models.CASCADE)
    rank = models.IntegerField(default=1)    

views.py

def is_valid_queryparam(param):
    return param != '' and param is not None

def search_filter_view(request):
    user_profile_games_filter = Game_Info.objects.all()
    user_profile = User_Info.objects.all()
    title_query = request.POST.get('q')  

    if is_valid_queryparam(title_query):
        user_profile_games_filter = user_profile_games_filter.filter(game_title__icontains=title_query)

    if request.user.is_authenticated:
        user_profile = User_Info.objects.filter(user=request.user)
        user_profile_game_obj = User_Info.objects.get(user=request.user)
        user_profile_rank = int(user_profile_game_obj.rank)


        user_profile_games_filter = Game_Info.objects.annotate(
            user_unlocked_game=Case(
                When(game_rank__lte=user_profile_rank, then=Value(True)),
                default=Value(False),
                output_field=BooleanField()
            )
        )

        context = {
            'user_profile': user_profile,  
            'user_profile_games_filter': user_profile_games_filter,
            'title_query' : title_query
        }

    else:
        context = {
            'user_profile': user_profile,
            'user_profile_games_filter': user_profile_games_filter,
            'title_query' : title_query
       }

    return render(request, "search_results.html", context)

search.html

<h1>Results for &#34;{{ title_query }}&#34;</h1>


            {% for content in user_profile_games_filter %}
                {% if content.user_unlocked_game %}
                        <!-- unlocked games logic -->
                        <a class="game-tile-container" href="{% url 'detail' content.pk %}">
                            <li class="results_info">
                                <img src= "{{ content.game_image.url }}"> 
                                <span class="results_title">{{ content.game_title }}</span>
                            </li>
                        </a>
        
                        {% else %}
                        <!-- locked games logic -->
                        <a class="game-tile-container" href="{% url 'detail' content.pk %}">
                            <li class="results_info">
                                <div class="locked_game">
                                    <img class="lock-img" src={% static 'images/treasure-chest-closed-alt.png' %} />
                                    <img src= "{{ content.game_image.url }}"> 
                                    <button class="level-up">Reach level {{ content.game_rank }} to unlock</button>
                                </div>
                                <span class="results_title">{{ content.game_title }}</span>
            
                            </li>
                        </a>
                {% endif %}
            {% endfor %}

Solution

  • Issue:

    user_profile_games_filter = Game_Info.objects.annotate(
                user_unlocked_game=Case(
                    When(game_rank__lte=user_profile_rank, then=Value(True)),
                    default=Value(False),
                    output_field=BooleanField()
                )
            )
    

    SOLUTION for anyone new; found after a long search and talk in the comments!:

    Create a new variable where you store a list of the ID's of the filtered objects, in this case is the variable user_profile_games_filter:

    NEW_VARIABLE = user_profile_games_filter.values_list('id', flat=True)
    

    Next, with the filtered objects, assign it to the desired object, making sure that you filter the id using id__in and it should be equal to the list of IDs. No loop needed!

    user_profile_games_filter = Game_Info.objects.filter(id__in=NEW_VARIABLE).annotate(...)