Skip to content
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

fix #78: Add CallableFilter.is_field and modify CallableFilter.lookups #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions url_filter/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import six
from cached_property import cached_property
from django import forms
from django.core.exceptions import ValidationError
from django.core.exceptions import ValidationError, FieldDoesNotExist
from django.db.models import Field

from .constants import StrictMode
from .fields import MultipleValuesField
Expand Down Expand Up @@ -476,14 +477,27 @@ def __init__(self, form_field=None, *args, **kwargs):
# need to overwrite to make form_field optional
super(CallableFilter, self).__init__(form_field, *args, **kwargs)

@cached_property
def is_field(self):
"""Return True if the attribute is instance of Field else return False.
"""
model = self.root.queryset.model
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root queryset is the top-level queryset but since filtersets can be nested model relations need to be followed to get the child model to check if it is a field. at this time there is no generic way of getting the child model which gets more complicated with other backends


try:
field = model._meta.get_field(self.name)
return True if isinstance(field, Field) else False
except FieldDoesNotExist:
return False

@cached_property
def lookups(self):
"""
Get all supported lookups for the filter

This property is identical to the super implementation except it also
finds all custom lookups from the class methods and adds them to the
set of supported lookups as returned by the super implementation.
This property is identical to the super implementation except it finds
all custom lookups from the class methods. And if the model attribute
is a field, adds them to the set of supported lookups as returned by
the super implementation. Else returns custom lookups only.
"""
lookups = super(CallableFilter, self).lookups

Expand All @@ -493,7 +507,10 @@ def lookups(self):
if m and m.groupdict()['backend'] == self.root.filter_backend.name
}

return lookups | custom_lookups
if self.is_field:
return lookups | custom_lookups
else:
return custom_lookups

def _get_filter_method_for_lookup(self, lookup):
name = 'filter_{}_for_{}'.format(lookup, self.root.filter_backend.name)
Expand Down