support for QuerySet.annotate() #12

Closed
rberiot opened this Issue Dec 7, 2012 · 4 comments

Projects

None yet

4 participants

@rberiot
rberiot commented Dec 7, 2012

It seems using QuerySet.annotate() breaks chartit. the problem lies at validation.py:30 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:
        pass
    if len(terms) == 1:
        try:
            return model._meta.get_field(terms[0]).verbose_name
        except:
            return str(terms[0])
@rochacbruno

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

@rochacbruno

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

https://gist.github.com/4536630

@JonPeel
Contributor
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.
#15

@bbratchiv

rberiot
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.
a086261
@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