视图.py
def search(request):
reportlist = []
loc_id = request.POST.get('location')
if loc_id:
location_list = ReportLocation.objects.filter(title=loc_id)
for locaton in location_list:
reportlist.append(locaton.report)
表格.py
class SearchFilterForm(Form):
location = forms.ChoiceField(widget=forms.Select(), choices='',required=False, initial='Your name')
def __init__(self,user_id, *args, **kwargs):
super(SearchFilterForm, self).__init__(*args, **kwargs)
self.fields['location'] = forms.ChoiceField(choices=[('','All Location types')]+[(loc.id, str(loc.title)) for loc in Location.objects.filter(user=user_id).exclude(parent_location_id=None)])
模型.py
class ReportLocation(models.Model):
report = models.ForeignKey(Report)
title = models.CharField('Location', max_length=200)
如何使用所选选项过滤 ReportLocation 字段中的标题字段。我尝试在 views.py 中使用上述过滤器查询,但未显示任何过滤数据。需要帮助