gpt4 book ai didi

python - Django 过滤器选择字段显示正确的值但不会过滤

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:17 24 4
gpt4 key购买 nike

我正在使用 Django 过滤器应用程序:https://github.com/carltongibson/django-filter

我正在制作一个网络漫画应用程序,我想要完成的是一个下拉过滤器字段,它显示该字段的不同值。在本例中,为系列字段。

阅读他们的文档后。以及堆栈溢出问题,如 this onethis one 。我可以使过滤器表单显示正确的下拉值。但是当我尝试按它们进行过滤时,我的结果查询集不返回任何值!

文档中肯定有一些我不理解的地方。这是我希望相关的代码。

模型.py
class ComicPanel(models.Model):
#... Other fields

series = models.CharField(max_length = 255, null = False, blank = False, default = getMainSeriesName)

# Other Filter Fields
chapter = models.FloatField(null = True, blank = True)

episode = models.IntegerField(null = True, blank = True)

class Meta:
constraints = [
models.UniqueConstraint(fields=['series','chapter', 'episode'])
]

def __str__(self):
return self.title

def save(self):
super(ComicPanel, self).save()

过滤器.py
import django_filters
from .models import ComicPanel

# Grab Distinct values for series
def getUniqueSeries():
series_dicts = ComicPanel.objects.all().values_list('series').distinct()
series_list = []
i = 0
for item in series_dicts:
series_list.append((i,item[0]))
i = i+1
return series_list

class ComicPanelFilter(django_filters.FilterSet):

series = django_filters.ChoiceFilter(choices = getUniqueSeries())

class Meta:
model = ComicPanel
fields = ['chapter', 'episode']

views.py
def view_archive(request):
comic_list = ComicPanel.objects.all()
comic_filter = ComicPanelFilter(request.GET, queryset=comic_list)
paginator = Paginator(comic_filter.qs, 8)

page = request.GET.get('page', 1)
try:
comics = paginator.page(page)
except (PageNotAnInteger, TypeError):
comics = paginator.page(1)
except EmptyPage:
comics = paginator.page(paginator.num_pages)

return render(request, 'comics/comic_archive.html', context = {'filter': comic_filter, 'comics': comics})

模板.html

...

<form class="form" method="GET">
{% csrf_token %}
<table class="my_classes">
<thead>
<tr>
<th scope="col">Filter Comics: </th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for field in filter.form %}
<tr>
<th scope="row"> {{ field.name }}</th>
<td>{{ field }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="btn btn-outline-dark">Filter</button>
</form>

...

{% if comics %}
{% for comic in comics %}
...
{% endfor %}

<!-- if no comics -->
{% else %}
<p class = "my_class"> Looks like you don't have any comics! Upload some? </p>
{% endif %}

结果:

我的过滤器表单显示下拉项的正确、独特的系列。但是当我按任何值过滤时,查询集不返回漫画

我还尝试在过滤器类中使用 ModelChoiceFilter ,得到类似的结果(但值以元组格式显示):

series=django_filters.ModelChoiceFilter(queryset=ComicPanel.objects.all().values_list('series').distinct())

有人可以告诉我我做错了什么吗?

最佳答案

我的错误实际上很愚蠢,

如果我们看一下Django docs for field.choices:

The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name.

所以这个方法:

# Grab Distinct values for series
def getUniqueSeries():
series_dicts = ComicPanel.objects.all().values_list('series').distinct()
series_list = []
i = 0
for item in series_dicts:
series_list.append((i,item[0]))
i = i+1
return series_list

返回一个列表,如下所示:[(0,'series1'), (1,'series2'), ...]

对于我的模型,元组的第一个值必须是系列名称。所以列表应该如下所示:[('系列1','系列1'),('系列2','系列2'),...]

所以简单的解决方法是更改​​这一行:

series_list.append((i,item[0]))

对此:

series_list.append((item[0],item[0]))

关于python - Django 过滤器选择字段显示正确的值但不会过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com