support for QuerySet.annotate() #12

rberiot opened this Issue Dec 7, 2012 · 4 comments


None yet

4 participants

rberiot commented Dec 7, 2012

It seems using QuerySet.annotate() breaks chartit. the problem lies at where it validates that the fields used are part of the model. meta.get_all_field_names() doesn't return annotated fields, for instance a Count() or an Avg() which are very useful in the case of a chart.
here's the offending code:

    model_fields = model._meta.get_all_field_names()
    if terms[0] not in model_fields:
        raise APIInputError("Field %r does not exist. Valid lookups are %s."
                         % (terms[0], ', '.join(model_fields)))
    if len(terms) == 1:
            return model._meta.get_field(terms[0]).verbose_name

as a temporary fix I changed it to this but it's just a dirty workarround and effectively breaks the validation:

    model_fields = model._meta.get_all_field_names()
    if terms[0] not in model_fields:
    if len(terms) == 1:
            return model._meta.get_field(terms[0]).verbose_name
            return str(terms[0])

+1 I am trying to do the same. I will use this workaround.


I needed to change otehr things in order to work with annotate and Count with datetime fields

JonPeel commented Apr 5, 2013

As an alternate/potential fix - inspect the aggregation functions and extra field names. Not perfect but at least avoids the pokemon exception.


Thank you very much for this tip!

@atodorov atodorov added a commit that closed this issue Aug 9, 2016
@atodorov atodorov Allow charting of extra() and annotate() fields. Fixes #8. Fixes #12
this is a slight refactoring of PR#15 to allow extra and annotated
fields be used not only in categories but other places as well.
@atodorov atodorov closed this in a086261 Aug 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment