【问题标题】:Django | FilterSet not running query姜戈 | FilterSet 未运行查询
【发布时间】:2020-12-10 11:24:31
【问题描述】:

我在将过滤器应用于 django 中的表时遇到了一些问题,当我应用过滤器(即单击“搜索”)时,没有任何反应。没有错误。没有崩溃。纳达。即使添加了我应用的搜索字段,该 url 确实发生了变化,该表仍然保持不变。

为了简单起见,我将重命名变量、模型和所有内容的原始名称。

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>

基本上,用户模型与模型 SomeThings 相关。后面的模型包含另一个用于 SomeStuff 和 OtherInfo 的 primary_key。

在表格上,正在显示来自 SomeStuff 和 OtherInfo 的信息,但是,我想仅使用 SomeStuff 中指定的变量 stuff_1 过滤表格。

【问题讨论】:

    标签: python django django-queryset django-filter django-filters


    【解决方案1】:

    我认为您必须将product 更改为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)
    

    【讨论】:

    • 傻我!就是这样。你介意解释一下这是为什么吗?
    • 过滤器将更新您在上下文中传递的查询集,如果没有搜索查询,则它将传递原始查询集。
    • 这真的很有帮助!
    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 2020-08-17
    • 2021-09-27
    • 2020-07-02
    • 2011-01-25
    • 2016-05-02
    • 2016-04-28
    • 2021-03-10
    相关资源
    最近更新 更多