[Fixed]-Catching a NotImplemented error during a distinct call in django

1👍

Querysets are evaluated lazily. Maybe the Exception is raised outside the try-except block?

You can force evaluation inside the try except in various ways.

try:
    _states = qs.distinct('registrationAuthority')
    bool(_states)  # evaluate queryset
    states = _states
except NotImplementedError:
    # do something to make a clean states variable here

Of note is that this won’t work:

try:
    states = qs.distinct('registrationAuthority')
    bool(states)  # evaluate queryset
except NotImplementedError:
    states = states.filter() # error raised here

As in the exception block, states will still have the distinct filter chained on the end, as it wasn’t stripped when the exception was caught.

Leave a comment