-
Notifications
You must be signed in to change notification settings - Fork 771
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
Filter method and lookup expressions #1199
Comments
Hi @Subaku. This isn't actually related to the class F(FilterSet):
name = CharFilter(field_name='username')
class Meta:
model = User
fields = {'name': ['exact', 'gt', 'lt']} However, there is a bug and filter generation doesn't warn you that the additional filters aren't generated. This has been fixed in #1061, but the change hasn't been released yet. That said, the fix is just raising a warning instead of silently failing. This kind of filter generation won't work, and you'll have to declare these filters manually. Additionally, you might be interested in #1150. One deficiency with the current filter |
Thanks for pointing out #1150. I'll try that workaround. Also sorry for not being clearer in my post. I'm aware you can't do what you show. I'm not trying to declare both a manual filter field AND the same field in the Meta. I'm doing either or. That said my proposed, possible bug still stands. I.e.
For whatever reason Thanks again. |
Right, it's not the declared filter that's an issue, it's generating the filters for the other lookups. In the example I posted above, it's the
You most likely have a validation error. Here's an example test case that I used to validate that def test_datetime_filter(self):
class F(FilterSet):
dt = DateTimeFilter(field_name='published', method='get_dt_now')
class Meta:
model = Article
fields = ['dt']
def get_dt_now(self, qs, name, value):
raise Exception('!!')
f = F({'dt': '2020-01-01 00:00:00'})
self.assertTrue(f.is_valid())
with self.assertRaisesMessage(Exception, ('!!')):
f.qs If you check your filter's |
Yup! It was a validation error. Thanks for your response :) |
Hi,
Seems like a potential bug. Declaring a method on a field in my FilterSet seems to not do anything under a couple of scenarios.
Am I just expecting too much of the method?
For reference I had the filter working before without a declared method by defining the following in my Meta:
Which worked great. I now need a method so I can customize what happens if the user filters by
__gt
or__gte
. So I figured I could simple remove thewhen_seen
from my meta and declare:when_seen = rest_filters.DateTimeFilter(field_name='when_seen', method='by_when_seen')
Yet no matter what I do my method does not get called. Any ideas?
The text was updated successfully, but these errors were encountered: