Search code examples
pythondjangodjango-querysetdjango-filterdjango-filters

Django | FilterSet not running query


I am having some trouble on applying a filter to a table in django, when I apply the filter (i.e. click on "Search") nothing happens. No error. No crash. Nada. The table stays the same as if nothing had happened eventhough the url does change adding the search fields that I applied.

I'll rename the original names of variables, models and everything in general for sake of simplicity.

models.py

from django.db import models
from other_app.models import CustomUser
from another_app.models import OtherModel

class SomeThings(models.Model):
    # default User was replaced with an AbstractUser model
    user = models.OneToOneField(CustomUser, on_delete=models.PROTECT) 
    id = models.CharField(max_length=10,primary_key=True)
    thing_1= models.PositiveIntegerField(blank=False)
    thing_2= models.PositiveIntegerField(blank=False)
    image= models.ImageField(null=True, blank=True)

class SomeStuff(models.Model):
    id = models.OneToOneField(SomeThings, on_delete=models.PROTECT)
    stuff_1 = models.CharField(max_length=255, blank=False)
    stuff_2 = models.CharField(max_length=255, blank=False)
    stuff_3 = models.ForeignKey(OtherModel, on_delete=models.PROTECT, null=True)

class OtherInfo(models.Model):
    id = models.OneToOneField(SomeThings, on_delete=models.PROTECT)
    character_1 = models.CharField(max_length=255, blank=False)
    character_2 = models.CharField(max_length=255, blank=False)

filters.py

import django_filters
from .models import *

class myFilter(django_filters.FilterSet):
    class Meta:
        model = SomeStuff
        fields = '__all__'
        exclude = ['stuff_2','stuff_3']

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .filters import myFilter

def search(request):
    products = SomeStuff.objects.all()

    filter= myFilter(request.GET,queryset=products)
    product= filter.qs

    context = {'products':products,'filter':filter,}
    return render(request, 'search.html', context)

search.html

{% load static %}

... some html stuff ...

<form method="get">
    {{ filter.form }}
    <button class="btn btn-primary" type="submit">
        Search
    </button>
</form>

<table>
    <thead>
        <tr>
            <th>Stuff 1</th>
            <th>Object 1</th>
            <th>Object 2</th>
        </tr>
    </thead>
    <tbody>
        {% for product in products %}
            <tr>
                <td>{{product.stuff_1}}</td>
                <td>{{product.id.otherinfo.character_1}}</td>
                <td>{{product.id.otherinfo.character_2}}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

Basically, the user model is related to the model SomeThings. This later model contains another primary_key which is used for SomeStuff and OtherInfo.

On the table, information from SomeStuff and OtherInfo is being displayed, however, I want to filter the table only using the variable stuff_1 specified in SomeStuff.


Solution

  • I think you have to change product with products

    def search(request):
        products = SomeStuff.objects.all()
    
        filter= myFilter(request.GET,queryset=products)
        products= filter.qs  #this should be products instead of product
    
        context = {'products':products,'filter':filter,}
        return render(request, 'search.html', context)