Skip to main content

A package for filtering any objects in django models

Project description

A package for filter all models in django based on query parameters.

Installation:

pip3 install django_rest_framework
pip3 install rest_filter_qp

if you use jalali date:

pip3 install jalali_date

query parameters:

https://example.com/[URL]/?filters[]=[{"key":"KEY1","value":"VALUE1","type":"TYPE"},{"key":"KEY2","value":"VALUE2",,"type":"TYPE"},...]

key : model field name

type : type of filters, you should write a type from these :

FILTER_TYPES = {
		'max':{ # max of a field
			'type' : 'int',
			'len' : 1,
			'extra_keys':[],
			'method' : Max,
			'is_aggregate':True
		},
		'min':{ # min of a field
			'type' : 'int',
			'len' : 1,
			'extra_keys':[],
			'method' : Min,
			'is_aggregate':True
		},
		'bwn':{ # field between 2 value
			'type' : 'list',
			'len' : 2,
			'extra_keys':['gte','lte'],
			'method' : None,
			'is_aggregate':False
		},
		'normal':{ # field equal a value 
			'type' : None,
			'len' : 1,
			'extra_keys':[],
			'method' : None,
			'is_aggregate':False
		},
		'today':{ # objects that created today
			'type' : 'list',
			'len'  : 1,
			'extra_keys' : ['year','month','day'],
			'method' : this_day,
			'is_aggregate':False
		},
		'toweek' : { # objects that created current week (based jalali date)
			'type':'rang',
			'len' : 1,
			'extra_keys' : ['range'],
			'method' : this_week,
			'is_aggregate':False
		},
		'tomonth' : { # objects that created current month
			'type' : 'list',
			'len' : 1,
			'extra_keys' : ['year','month'],
			'method' : this_month,
			'is_aggregate':False
		},
		'asc' : { # order asc
			'type' : 'sort',
			'len' : 1,
			'extra_keys' : [],
			'method' : None,
			'is_aggregate' : False
		},
		'desc' : { # order desc
			'type' : 'sort',
			'len' : 1,
			'extra_keys' : [],
			'method' : None,
			'is_aggregate' : False
		}
	}

for example, you have a model like this :

class Product(models.Model):
    title = models.CharField(max_length=255)
    by_count = models.IntegerField(default=0)
    view_count = models.IntegerField(default=0)
    slug = models.SlugField(max_length=255,unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

you should add FilterParams class from rest_filter_qp to your filter_backends[]:

from rest_filter_qp import FilterParams
class ProductView(generics.ListAPIView):
	queryset = Product.objects.all()
	serializer_class = ProductSerializer
	filter_backends = [FilterParams]

now, you want filter product model by title field that value is "Foo" and created today ::

[YOUR_DOMAIN]/products/?filters[]=[{"key":"title","value":"Foo","type":"normal"},{"key":"created_at","value":"","type":"today"}]

if you want a objects with maximum value :

[YOUR_DOMAIN]/products/?filters[]=[{"key":"view_count","value":"","type":"max"}]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rest-filter-qp-1.2.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rest_filter_qp-1.2.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file rest-filter-qp-1.2.1.tar.gz.

File metadata

  • Download URL: rest-filter-qp-1.2.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for rest-filter-qp-1.2.1.tar.gz
Algorithm Hash digest
SHA256 518b96ecca7e1a1feb343b55bec2a1425dc55155fd9a2d83ebce960951d5df65
MD5 d5dc7d9d5b3669e53df1779348808418
BLAKE2b-256 e6b6fe18c69088075bfa65c295ed19f214ee0ba07cbc2a1366aa4bf5f39dfdbe

See more details on using hashes here.

File details

Details for the file rest_filter_qp-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: rest_filter_qp-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for rest_filter_qp-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcda12fcb2c6dfbedab5e04a9202b8eb55fd25f5f70fffba5b6fde0f083210ac
MD5 2604bfc25b29d88c791a16ecb221a80d
BLAKE2b-256 5b32e73e9106b050d420c6d22c6d4f2466bf35cb021038870291146bc6bff393

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page