Is there any way to get the "filter name" inside the common custom filter function defined against it #1840
-
I have written generic custom filters. def common_custom_filter_func(**args, **kwargs):
print(filter name) And there are many filters with different names that are mapped to the same function
Is there any way I can get my filter name inside the filter function when it's called? For example, I need to catch the name filter1 inside
I tried pass_context but it does not have any attribute that has the caller filter name. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
No, that name isn't even known to Python itself since it's not the function name but just some dict key it's assigned to. Here's a workaround: filters = {
"filter1": common_custom_filter_func,
"filter2": common_custom_filter_func,
}
filters = {k: functools.partial(v, k) for k, v in filters.items()} Then just add a new positional argument to be beginning of your function, e.g. |
Beta Was this translation helpful? Give feedback.
-
@ThiefMaster Thank you so much for the quick reply, It works. |
Beta Was this translation helpful? Give feedback.
No, that name isn't even known to Python itself since it's not the function name but just some dict key it's assigned to.
Here's a workaround:
Then just add a new positional argument to be beginning of your function, e.g.
def common_filter(name, whatever, else)
and use that argument to get the name.