chartit / django-chartit

A Django app to plot charts and pivot charts directly from the models. Uses HighCharts and jQuery JavaScript libraries to render the charts on the webpage.
http://django-chartit.mrsenko.com/
Other
492 stars 164 forks source link

support for QuerySet.annotate() #12

Closed rberiot closed 8 years ago

rberiot commented 11 years ago

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 commented 11 years ago

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

rochacbruno commented 11 years ago

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

https://gist.github.com/4536630

JonPeel commented 11 years ago

As an alternate/potential fix - inspect the aggregation functions and extra field names. Not perfect but at least avoids the pokemon exception. https://github.com/pgollakota/django-chartit/pull/15

bbratchiv commented 8 years ago

rberiot Thank you very much for this tip!