Home > other >  Django Filter all fields of Model
Django Filter all fields of Model

Time:09-06

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

CodePudding user response:

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) | ...)
  • Related