drf-complex-filter


Namedrf-complex-filter JSON
Version 2.0.1 PyPI version JSON
download
home_pageNone
SummaryDeclarative filter for Django ORM
upload_time2024-12-16 11:01:37
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License Copyright (c) 2020 Nikita Balobanov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords django django-rest-framework django-orm
VCS
bugtrack_url
requirements django
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django Rest Framework Complex Filter

[![codecov](https://codecov.io/gh/kit-oz/drf-complex-filter/branch/main/graph/badge.svg?token=B6Z1LWBXOP)](https://codecov.io/gh/kit-oz/drf-complex-filter)
[![PyPI version](https://badge.fury.io/py/drf-complex-filter.svg)](https://badge.fury.io/py/drf-complex-filter)
[![Python Versions](https://img.shields.io/pypi/pyversions/drf-complex-filter.svg)](https://pypi.org/project/drf-complex-filter/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A powerful and flexible declarative filter for Django ORM that enables complex query construction through a simple JSON-based API. Perfect for building advanced filtering capabilities in your Django REST Framework applications.

## Features

- **Advanced Filtering**: Complex AND/OR operations with nested conditions
- **Declarative Syntax**: Simple JSON-based query structure
- **Dynamic Values**: Support for computed values and server-side calculations
- **Related Model Queries**: Efficient subquery handling for related models
- **Extensible**: Easy to add custom operators and value functions
- **Type Safe**: Built-in operator validation
- **DRF Integration**: Seamless integration with Django REST Framework

## Installation

```bash
pip install drf-complex-filter
```

## Quick Start

1. Add `ComplexQueryFilter` to your ViewSet:

```python
from drf_complex_filter.filters import ComplexQueryFilter

class UserViewSet(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = [ComplexQueryFilter]
```

2. Make API requests with complex filters:

```bash
# Simple equality filter
GET /users?filters={"type":"operator","data":{"attribute":"first_name","operator":"=","value":"John"}}

# Complex AND condition
GET /users?filters={"type":"and","data":[
    {"type":"operator","data":{"attribute":"age","operator":">","value":18}},
    {"type":"operator","data":{"attribute":"is_active","operator":"=","value":true}}
]}
```

## Filter Types

### 1. Simple Operator
```python
{
    "type": "operator",
    "data": {
        "attribute": "field_name",
        "operator": "=",
        "value": "value_to_compare"
    }
}
```

### 2. AND Operator
```python
{
    "type": "and",
    "data": [
        # List of operators to combine with AND
    ]
}
```

### 3. OR Operator
```python
{
    "type": "or",
    "data": [
        # List of operators to combine with OR
    ]
}
```

## Available Operators

| Operator | Description | Symbol |
|----------|-------------|---------|
| Is | Equality | = |
| Is not | Inequality | != |
| Contains | Case-insensitive contains | * |
| Not contains | Case-insensitive not contains | ! |
| Greater | Greater than | > |
| Greater or equal | Greater than or equal | >= |
| Less | Less than | < |
| Less or equal | Less than or equal | <= |
| In | Value in list | in |
| Not in | Value not in list | not_in |
| Current user | Current authenticated user | me |
| Not current user | Not current authenticated user | not_me |

## Advanced Features

### Custom Operators

1. Create your operator class:
```python
class CustomOperators:
    def get_operators(self):
        return {
            "custom_op": lambda f, v, r, m: Q(**{f"{f}__custom": v}),
        }
```

2. Register in settings:
```python
COMPLEX_FILTER_SETTINGS = {
    "COMPARISON_CLASSES": [
        "drf_complex_filter.comparisons.CommonComparison",
        "path.to.CustomOperators",
    ],
}
```

### Dynamic Values

1. Create value functions:
```python
class CustomFunctions:
    def get_functions(self):
        return {
            "current_time": lambda request, model: timezone.now(),
        }
```

2. Register in settings:
```python
COMPLEX_FILTER_SETTINGS = {
    "VALUE_FUNCTIONS": [
        "drf_complex_filter.functions.DateFunctions",
        "path.to.CustomFunctions",
    ],
}
```

3. Use in filters:
```python
{
    "type": "operator",
    "data": {
        "attribute": "created_at",
        "operator": ">",
        "value": {
            "func": "current_time",
            "kwargs": {}
        }
    }
}
```

### Related Model Queries

Use `ModelName___` prefix for efficient subqueries:
```python
{
    "type": "operator",
    "data": {
        "attribute": "Profile___is_verified",
        "operator": "=",
        "value": true
    }
}
```

## Requirements

- Python >= 3.6
- Django >= 3.0.0
- Django REST Framework

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "drf-complex-filter",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "django, django-rest-framework, django-orm",
    "author": null,
    "author_email": "Nikita Balobanov <kit-oz@ya.ru>",
    "download_url": "https://files.pythonhosted.org/packages/79/ad/f5d0c9674b55efa41770f78294c21a02ec8499103a4810db5d5f7dc57713/drf_complex_filter-2.0.1.tar.gz",
    "platform": null,
    "description": "# Django Rest Framework Complex Filter\n\n[![codecov](https://codecov.io/gh/kit-oz/drf-complex-filter/branch/main/graph/badge.svg?token=B6Z1LWBXOP)](https://codecov.io/gh/kit-oz/drf-complex-filter)\n[![PyPI version](https://badge.fury.io/py/drf-complex-filter.svg)](https://badge.fury.io/py/drf-complex-filter)\n[![Python Versions](https://img.shields.io/pypi/pyversions/drf-complex-filter.svg)](https://pypi.org/project/drf-complex-filter/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA powerful and flexible declarative filter for Django ORM that enables complex query construction through a simple JSON-based API. Perfect for building advanced filtering capabilities in your Django REST Framework applications.\n\n## Features\n\n- **Advanced Filtering**: Complex AND/OR operations with nested conditions\n- **Declarative Syntax**: Simple JSON-based query structure\n- **Dynamic Values**: Support for computed values and server-side calculations\n- **Related Model Queries**: Efficient subquery handling for related models\n- **Extensible**: Easy to add custom operators and value functions\n- **Type Safe**: Built-in operator validation\n- **DRF Integration**: Seamless integration with Django REST Framework\n\n## Installation\n\n```bash\npip install drf-complex-filter\n```\n\n## Quick Start\n\n1. Add `ComplexQueryFilter` to your ViewSet:\n\n```python\nfrom drf_complex_filter.filters import ComplexQueryFilter\n\nclass UserViewSet(ModelViewSet):\n    queryset = User.objects.all()\n    serializer_class = UserSerializer\n    filter_backends = [ComplexQueryFilter]\n```\n\n2. Make API requests with complex filters:\n\n```bash\n# Simple equality filter\nGET /users?filters={\"type\":\"operator\",\"data\":{\"attribute\":\"first_name\",\"operator\":\"=\",\"value\":\"John\"}}\n\n# Complex AND condition\nGET /users?filters={\"type\":\"and\",\"data\":[\n    {\"type\":\"operator\",\"data\":{\"attribute\":\"age\",\"operator\":\">\",\"value\":18}},\n    {\"type\":\"operator\",\"data\":{\"attribute\":\"is_active\",\"operator\":\"=\",\"value\":true}}\n]}\n```\n\n## Filter Types\n\n### 1. Simple Operator\n```python\n{\n    \"type\": \"operator\",\n    \"data\": {\n        \"attribute\": \"field_name\",\n        \"operator\": \"=\",\n        \"value\": \"value_to_compare\"\n    }\n}\n```\n\n### 2. AND Operator\n```python\n{\n    \"type\": \"and\",\n    \"data\": [\n        # List of operators to combine with AND\n    ]\n}\n```\n\n### 3. OR Operator\n```python\n{\n    \"type\": \"or\",\n    \"data\": [\n        # List of operators to combine with OR\n    ]\n}\n```\n\n## Available Operators\n\n| Operator | Description | Symbol |\n|----------|-------------|---------|\n| Is | Equality | = |\n| Is not | Inequality | != |\n| Contains | Case-insensitive contains | * |\n| Not contains | Case-insensitive not contains | ! |\n| Greater | Greater than | > |\n| Greater or equal | Greater than or equal | >= |\n| Less | Less than | < |\n| Less or equal | Less than or equal | <= |\n| In | Value in list | in |\n| Not in | Value not in list | not_in |\n| Current user | Current authenticated user | me |\n| Not current user | Not current authenticated user | not_me |\n\n## Advanced Features\n\n### Custom Operators\n\n1. Create your operator class:\n```python\nclass CustomOperators:\n    def get_operators(self):\n        return {\n            \"custom_op\": lambda f, v, r, m: Q(**{f\"{f}__custom\": v}),\n        }\n```\n\n2. Register in settings:\n```python\nCOMPLEX_FILTER_SETTINGS = {\n    \"COMPARISON_CLASSES\": [\n        \"drf_complex_filter.comparisons.CommonComparison\",\n        \"path.to.CustomOperators\",\n    ],\n}\n```\n\n### Dynamic Values\n\n1. Create value functions:\n```python\nclass CustomFunctions:\n    def get_functions(self):\n        return {\n            \"current_time\": lambda request, model: timezone.now(),\n        }\n```\n\n2. Register in settings:\n```python\nCOMPLEX_FILTER_SETTINGS = {\n    \"VALUE_FUNCTIONS\": [\n        \"drf_complex_filter.functions.DateFunctions\",\n        \"path.to.CustomFunctions\",\n    ],\n}\n```\n\n3. Use in filters:\n```python\n{\n    \"type\": \"operator\",\n    \"data\": {\n        \"attribute\": \"created_at\",\n        \"operator\": \">\",\n        \"value\": {\n            \"func\": \"current_time\",\n            \"kwargs\": {}\n        }\n    }\n}\n```\n\n### Related Model Queries\n\nUse `ModelName___` prefix for efficient subqueries:\n```python\n{\n    \"type\": \"operator\",\n    \"data\": {\n        \"attribute\": \"Profile___is_verified\",\n        \"operator\": \"=\",\n        \"value\": true\n    }\n}\n```\n\n## Requirements\n\n- Python >= 3.6\n- Django >= 3.0.0\n- Django REST Framework\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2020 Nikita Balobanov  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Declarative filter for Django ORM",
    "version": "2.0.1",
    "project_urls": {
        "Homepage": "https://github.com/kit-oz/drf-complex-filter"
    },
    "split_keywords": [
        "django",
        " django-rest-framework",
        " django-orm"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0427a92df429c913d72d8bc280ad616308db6f7719d94283ba047744d276cd6c",
                "md5": "b137bd48d724dcaa6cdaa79baba7f7ed",
                "sha256": "d41d0d81a0c0dea3d8d909a13f6a386ea7c15a7bc0362416ed8e31da807e1596"
            },
            "downloads": -1,
            "filename": "drf_complex_filter-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b137bd48d724dcaa6cdaa79baba7f7ed",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 10945,
            "upload_time": "2024-12-16T11:01:33",
            "upload_time_iso_8601": "2024-12-16T11:01:33.948271Z",
            "url": "https://files.pythonhosted.org/packages/04/27/a92df429c913d72d8bc280ad616308db6f7719d94283ba047744d276cd6c/drf_complex_filter-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79adf5d0c9674b55efa41770f78294c21a02ec8499103a4810db5d5f7dc57713",
                "md5": "c034426c2e32492b2d69e3f2e36f6db4",
                "sha256": "e9b4a5d11335442d7076fa2583298ae368405aec099cd99b5c93b4c77fc1d5e2"
            },
            "downloads": -1,
            "filename": "drf_complex_filter-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c034426c2e32492b2d69e3f2e36f6db4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 18621,
            "upload_time": "2024-12-16T11:01:37",
            "upload_time_iso_8601": "2024-12-16T11:01:37.627678Z",
            "url": "https://files.pythonhosted.org/packages/79/ad/f5d0c9674b55efa41770f78294c21a02ec8499103a4810db5d5f7dc57713/drf_complex_filter-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-16 11:01:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kit-oz",
    "github_project": "drf-complex-filter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "django",
            "specs": [
                [
                    ">=",
                    "3.0.0"
                ]
            ]
        }
    ],
    "lcname": "drf-complex-filter"
}
        
Elapsed time: 0.38748s