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

Issue 42 - Generic solution for filtering over relationships #49

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
41 changes: 29 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class UserFilter(FilterSet):
fields = {
'username': ['eq', 'ne', 'in', 'ilike'],
'is_active': [...], # shortcut!
'assignments': { # (nested) relationships are supported
'task': {
'name': ['eq'],
},
'active': ['eq']
}
}

@staticmethod
Expand All @@ -45,7 +51,12 @@ Now, we're going to create query.
or: [
{isAdmin: true},
{usernameIn: ["moderator", "cool guy"]}
]
],
assignments: {
task: {
name: "Write code"
}
}
}
){
edges {
Expand All @@ -65,10 +76,10 @@ Now, we're going to create query.

FilterSet class must inherit `graphene_sqlalchemy_filter.FilterSet` or your subclass of this class.

There are three types of filters:
1. [automatically generated filters](#automatically-generated-filters)
1. [simple filters](#simple-filters)
1. [filters that require join](#filters-that-require-join)
There are three types of filters:
1. [automatically generated filters](#automatically-generated-filters)
1. [simple filters](#simple-filters)
1. [filters that require join](#filters-that-require-join)


## Automatically generated filters
Expand All @@ -77,14 +88,20 @@ class UserFilter(FilterSet):
class Meta:
model = User
fields = {
'username': ['eq', 'ne', 'in', 'ilike'],
'is_active': [...], # shortcut!
'username': ['eq', 'ne', 'in', 'ilike'],
'is_active': [...], # shortcut!
'assignments': { # (nested) relationships are supported
'task': {
'name': ['eq'],
},
'active': ['eq']
}
}
```
Metaclass must contain the sqlalchemy model and fields.

Automatically generated filters must be specified by `fields` variable.
Key - field name of sqlalchemy model, value - list of expressions (or shortcut).
Automatically generated filters must be specified by `fields` variable.
Key - field name of sqlalchemy model, value - list of expressions (or shortcut). For relationship fields, the value is another dictionary defining the filters for the related model.

Shortcut (default: `[...]`) will add all the allowed filters for this type of sqlalchemy field (does not work with hybrid property).

Expand Down Expand Up @@ -141,7 +158,7 @@ class UserFilter(FilterSet):
@classmethod
def is_moderator_filter(cls, info, query, value):
membership = cls.aliased(query, Membership, name='is_moderator')

query = query.outerjoin(
membership,
and_(
Expand Down Expand Up @@ -207,7 +224,7 @@ class UserFilter(FilterSet):
class Meta:
model = User
fields = {'is_active': [...]}



class CustomField(FilterableConnectionField):
Expand Down Expand Up @@ -431,7 +448,7 @@ class MyString(types.String):
class BaseFilter(FilterSet):
# You can override all allowed filters
# ALLOWED_FILTERS = {types.Integer: ['eq']}

# Or add new column type
EXTRA_ALLOWED_FILTERS = {MyString: ['eq']}

Expand Down
Loading