我正在尝试按开始日期过滤我的搜索结果。然而,开始日期存储为 ForeignKey 对象的字段。
楷模
class Observation(models.Model):
taxon_node = models.ForeignKey(TaxonNode)
sample = models.ForeignKey(Sample)
remarks = models.TextField(blank=True)
class Sample(models.Model):
name = models.CharField(max_length=255)
timespan_begin = models.DateTimeField()
搜索索引
class ObservationIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
taxon_node = indexes.CharField(model_attr='taxon_node')
sample_timespan_begin = indexes.CharField(model_attr='sample__timespan_begin')
形式
class ObservationSearchForm(SearchForm):
start_date = forms.DateTimeField(required=False)
def search(self):
sqs = super(ObservationSearchForm, self).search()
if not self.is_valid():
return self.no_query_found()
if self.cleaned_data['start_date']:
sqs = sqs.filter(sample_timespan_begin__gte=self.cleaned_data['start_date'])
return sqs
指数
{{ object.taxon_node }}
{{ object.sample.timespan_begin }}
{{ object.remarks }}
我无法弄清楚问题出在哪里。应用过滤器后 sqs 不会改变。我的观察文本有问题还是 sqs.filter() 无效?