views.py過濾器使用Django過濾器從數據庫中的數據
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)
forms.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)])
models.py
class ReportLocation(models.Model):
report = models.ForeignKey(Report)
title = models.CharField('Location', max_length=200)
如何ReportLocation場過濾標題字段與選擇的選擇。我嘗試在views.py上面的過濾器查詢,但它沒有顯示任何過濾的數據。需要幫助
您的標題包含id字段?這很奇怪... –