Python Django |过滤器集未运行查询
在django中将筛选器应用于表时遇到了一些问题,当我应用筛选器(即单击“搜索”)时,什么都没有发生。没有错误。没有撞车。娜达。即使url在添加我应用的搜索字段时发生了更改,该表仍保持不变,就好像什么都没有发生一样 为了简单起见,我将重命名变量、模型和所有东西的原始名称 型号.pyPython Django |过滤器集未运行查询,python,django,django-queryset,django-filter,django-filters,Python,Django,Django Queryset,Django Filter,Django Filters,在django中将筛选器应用于表时遇到了一些问题,当我应用筛选器(即单击“搜索”)时,什么都没有发生。没有错误。没有撞车。娜达。即使url在添加我应用的搜索字段时发生了更改,该表仍保持不变,就好像什么都没有发生一样 为了简单起见,我将重命名变量、模型和所有东西的原始名称 型号.py from django.db import models from other_app.models import CustomUser from another_app.models import OtherMod
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)
import django_filters
from .models import *
class myFilter(django_filters.FilterSet):
class Meta:
model = SomeStuff
fields = '__all__'
exclude = ['stuff_2','stuff_3']
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)
过滤器.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)
import django_filters
from .models import *
class myFilter(django_filters.FilterSet):
class Meta:
model = SomeStuff
fields = '__all__'
exclude = ['stuff_2','stuff_3']
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)
视图.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)
import django_filters
from .models import *
class myFilter(django_filters.FilterSet):
class Meta:
model = SomeStuff
fields = '__all__'
exclude = ['stuff_2','stuff_3']
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>
{%load static%}
... 一些html的东西。。。
{{filter.form}
搜寻
材料1
对象1
对象2
{products%中产品的%s}
{{product.stuff_1}}
{{product.id.otherinfo.character_1}
{{product.id.otherinfo.character_2}
{%endfor%}
基本上,用户模型是与模型相关的。后一个模型包含另一个主键,用于something和OtherInfo
在表上,显示来自SomeStuff和OtherInfo的信息,但是,我只想使用SomeStuff中指定的变量stuff_1筛选表。我认为您必须使用
产品更改产品
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)
我认为你必须用产品
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)
我真傻!就这样。你介意解释一下原因吗?过滤器会更新你在上下文中传递的查询集,如果没有搜索查询,它会传递原始查询集。这真的很有帮助!我真傻!就这样。你介意解释一下原因吗?过滤器会更新你在上下文中传递的查询集,如果没有搜索查询,它会传递原始查询集。这真的很有帮助!