我在將過濾器應用于 django 中的表時遇到了一些麻煩,當我應用過濾器(即單擊“搜索”)時沒有任何反應。沒有錯誤。沒有崩潰。納達。該表保持不變,就好像什么都沒發生一樣,盡管 url 確實改變了添加我應用的搜索字段。為了簡單起見,我將重命名變量、模型和所有內容的原始名稱。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 相關。后面的模型包含另一個用于 SomeStuff 和 OtherInfo 的 primary_key。在表格上,正在顯示來自 SomeStuff 和 OtherInfo 的信息,但是,我只想使用 SomeStuff 中指定的變量 stuff_1 來過濾表格。
1 回答

BIG陽
TA貢獻1859條經驗 獲得超6個贊
我認為你必須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)
添加回答
舉報
0/150
提交
取消