我正在尝试按开始日期过滤搜索结果。但是,开始日期存储为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不会更改。我的observation_text有问题还是sqs.filter()无效?
答案 0 :(得分:0)
固定版本的sqs =
sqs = SearchQuerySet().filter(sample_timespan_begin__gte=self.cleaned_data['start_date'])