-
Notifications
You must be signed in to change notification settings - Fork 77
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
JSON fields #26
Comments
Hi @Goomba41 , thanks for your request. It is currently not supported by the library but it'd be a nice addition to it if we can integrate it. I am going to start looking into it and will keep you posted here. |
What I ended up doing is using the hybrid_property decorators to "recreate" the fields from the JSON field. This then allows the filter function to work.
Its not ideal since you have to define them with each change, but it works |
@juliotrigo is there any progress on this |
Looks like these changes might work for jsonb parsing in sorting.py and filtering.py , I have added below code
|
Here's a simple patch for anyone who ends up here for JSONB import sqlalchemy_filters
import types
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy_filters import ( # noqa
apply_filters,
apply_loads,
apply_pagination,
apply_sort,
filters,
loads,
models,
pagination,
sorting,
)
class JSONPatchField(models.Field):
def get_sqlalchemy_field(self):
sub_field_names = None
if "." in self.field_name:
field_name, *sub_field_names = self.field_name.split(".")
else:
field_name = self.field_name
if field_name not in self._get_valid_field_names():
raise models.FieldNotFound(
"Model {} has no column `{}`.".format(self.model, field_name)
)
sqlalchemy_field = getattr(self.model, field_name)
# If it's a hybrid method, then we call it so that we can work with
# the result of the execution and not with the method object itself
if isinstance(sqlalchemy_field, types.MethodType):
sqlalchemy_field = sqlalchemy_field()
elif isinstance(sqlalchemy_field.type, JSONB) and sub_field_names:
sqlalchemy_field = sqlalchemy_field[sub_field_names]
sqlalchemy_field = sqlalchemy_field.astext
return sqlalchemy_field
sqlalchemy_filters.filters.Field = JSONPatchField
sqlalchemy_filters.loads.Field = JSONPatchField
sqlalchemy_filters.pagination.Field = JSONPatchField
sqlalchemy_filters.sorting.Field = JSONPatchField |
Is it possible to support the filtering function by the key values of JSON fields?
SQLAlchemy has a JSON_EXTRACT function and I can filter Model.json_column ['key'] == value
The text was updated successfully, but these errors were encountered: