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

Django Filter all fields of Model

i have a short Question.

I’ve been working on a Inventory-System to use in my company. So i’m pretty new here and i’m not experienced with Django.

To filter the items, i made a simple search bar, where you can search for items. The filter looks like this:

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

return Item.objects.filter(name__contains=self.request.GET.get('search_item'))

As you see, i only filter by name, but i would like to filter all attributes of Profile with the search-field. Is it possible to check all fields of a Model in a single query?

Thank you

>Solution :

You can do it using django-filter library, or if you want to do it manually, then this can be achieved using Q objects:

from django.db.models import Q

search_item = self.request.GET.get('search_item')
Item.objects.filter(Q(name=search_item) | Q(other_field=search_item) | ...)
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