-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
TypeError in schema generation when using a django_filters.ChoiceFilter with a callable "choices" #688
Comments
Hi @glennmatthews, interesting! This has never come up before. I created a small addition to our tests def choice_callable():
return [('A', 'a'), ('b', 'b')]
class ProductFilter(FilterSet):
...
choice_func = ChoiceFilter(field_name='category', choices=choice_callable) # won't work without field_name The Since we must have a model field, we could still get a choice list from the model field (if applicable). The result of the callable probably must be a subset of the choices in the model field The question is what makes the most sense here? We could attempt to get a list but it could be misleading since we can only show the full list while the callable allows only a subset. We could either show the whole list (if we have one) or clear the enum altogether. What do you think? FYI: you can override the detection like so. The
|
Thanks for the speedy response! I'd come to the same conclusion regarding the test addition - glad to see I'm on the right track with trying to reproduce and fix this! In my opinion, the most correct behavior would be to clear the enum in this case, since we don't have an easy way to know exactly which values are in fact permitted. I'll get a PR open shortly with my attempt at implementing such logic. Thanks for the tip on the workaround as well - I'll give that a try with our project in the meantime. |
I agree, better to be defensive but correct. hehe thanks... I had the same changes already staged. just a minor comment over there. |
awesome stuff, thanks again. just to give you a perspective. since we just released 2 days ago it will be roughly 2-3 weeks depending on the amount of issues coming up in the meantime. |
@tfranzel Would you mind releasing a new version soon? We just got tripped up not being able to publish out project to PyPI by referencing a non-packaged dependency: https://github.com/nautobot/nautobot/runs/6067093468?check_suite_focus=true |
@bryanculver, yeah that won't work 😄 As a matter of fact this got further improved on master. I have a backlog of 2-3 issues I have to take a look at. Release is coming in the following days. Won't be too long. |
0.22.1 released |
Describe the bug
A django-filters
ChoiceFilter
takes a parameterchoices
which can either be a list of valid values, or a function/callable which when called yields such a listing. The latter case is useful when the choices need to be determined at execution time rather than at declaration time. However, when using a filterset that contains filters with callablechoices
, drf-spectacular schema generation throws a TypeError:Possibly fixing this would be as simple as adding an
if not callable(filter_field.extra['choices'])
check before attempting to evaluate it as a list, and perhaps providing some fallback logic if that check fails?To Reproduce
I don't have a minimized example at hand, my apologies, but hopefully the above description provides sufficient context.
Expected behavior
Schema generation should succeed; since the choices are dynamically generated at evaluation time, the generated schema presumably may not be able to provide a defined list of choices.
The text was updated successfully, but these errors were encountered: