Django Filter all fields of Model

Question:

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:

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

Asked By: whoami0605

||

Answers:

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) | ...)
Answered By: Ersain
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.