我在將過濾器應(yīng)用于 django 中的表時(shí)遇到了一些麻煩,當(dāng)我應(yīng)用過濾器(即單擊“搜索”)時(shí)沒有任何反應(yīng)。沒有錯(cuò)誤。沒有崩潰。納達(dá)。該表保持不變,就好像什么都沒發(fā)生一樣,盡管 url 確實(shí)改變了添加我應(yīng)用的搜索字段。為了簡單起見,我將重命名變量、模型和所有內(nèi)容的原始名稱。models.pyfrom django.db import modelsfrom other_app.models import CustomUserfrom another_app.models import OtherModelclass 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.pyimport django_filtersfrom .models import *class myFilter(django_filters.FilterSet):? ? class Meta:? ? ? ? model = SomeStuff? ? ? ? fields = '__all__'? ? ? ? exclude = ['stuff_2','stuff_3']views.pyfrom django.shortcuts import renderfrom django.http import HttpResponsefrom .filters import myFilterdef 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)基本上,用戶模型與模型 SomeThings 相關(guān)。后面的模型包含另一個(gè)用于 SomeStuff 和 OtherInfo 的 primary_key。在表格上,正在顯示來自 SomeStuff 和 OtherInfo 的信息,但是,我只想使用 SomeStuff 中指定的變量 stuff_1 來過濾表格。
1 回答

BIG陽
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為你必須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)
添加回答
舉報(bào)
0/150
提交
取消