Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Wagtailsearch doesn't deal with annotations in querysets #2340

Open
danni opened this Issue Mar 11, 2016 · 0 comments

Comments

Projects
None yet
3 participants
Contributor

danni commented Mar 11, 2016

Given a model where you've used a callable to generate a FilterField:

class MyModel(models.Model, index.Indexable):
    ...

    @property
    def duration(self):
        return self.end_date - self.start_date

    search_fields = (
        index.FilterField('duration'),
    )

Django expects you to have annotated this field into your queryset in order to query on it.

class DurationInMonths(Func):
    template = \
        'EXTRACT(year FROM age(%(expressions)s)) * 12 + ' \
        'EXTRACT(month FROM age(%(expressions)s))'
    output_field = fields.IntegerField()

queryset = queryset.annotate(duration=DurationInMonths(F('end_date'), F('start_date'))

This generates an exception in Wagtail:

AttributeError: 'DurationInMonths' object has no attribute 'target'

If you add a target field (not strictly correct as I understand the docs).

AttributeError: 'IntegerField' object has no attribute 'attname'

It should be reading the attribute name from the annotate method I think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment