New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for QuerySet.annotate() #12

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

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

This comment has been minimized.

rochacbruno commented Jan 15, 2013

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

@rochacbruno

This comment has been minimized.

rochacbruno commented Jan 15, 2013

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

https://gist.github.com/4536630

@JonPeel

This comment has been minimized.

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

This comment has been minimized.

bbratchiv commented Jan 18, 2016

rberiot
Thank you very much for this tip!

@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