Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use or for filter combination

I have two query like this below,

Both,It works correctly, but I want to use OR for these sentences.

I have ideas to use Q

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But it has complex filter.

cls.objects.filter(point1__gt=0).filter(point1__lt=100)

cld.objects.filter(point2__gt=0).filter(point2__lt=100)

Is there any method to use OR for this sentenses?

>Solution :

You can try different ORM methods here, take a glance below:

cls.objects.filter(point1__range=(0, 100))
cld.objects.filter(point2__range=(0, 100))

or you can also do something like:

cls.objects.filter(point1__in=[0, 100])
cld.objects.filter(point2__in=[0, 100])

NOTE that you can do the same with Q function, you said OR above, I think you need AND here.

Links for Django official documentation:
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#range
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#in

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading