Query questions. Please help me.

24 views
Skip to first unread message

나르엔

unread,
Jul 17, 2019, 4:04:16 PM7/17/19
to Django users
Hello, I am studying django.

When I use objects.filter, I want to search various columns. 

Columns to search for are unknown and have been receiving columns to search through checkbox in html.

What should I do? Please teach me.
It's blocked from below.
selectoptions is name in checkbox.


-------
def get_queryset(self):
        context = super(SearchListView, self).get_queryset()
        query = self.request.GET.get('q')
        selectOptions = self.request.GET.getlist('selectoptions')

Jani Tiainen

unread,
Jul 17, 2019, 4:42:01 PM7/17/19
to django...@googlegroups.com
Hi.

You can use dictionary unpacking to kwargs.

.filter(**{"name__contains": "foo", "somefield__gt": 123})



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dbdb681d-e7ad-44db-a825-512cc9436575%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

나르엔

unread,
Jul 17, 2019, 10:11:28 PM7/17/19
to Django users

Thank you!
That's working! But I think it's just a AND condition. It's embarrassing, but what should I do with the OR conditions?

def get_queryset(self):
context = super(SearchListView, self).get_queryset()
query = self.request.GET.get('q')
selectOptions = self.request.GET.getlist('selectoptions')
search_querys = dict((i,query) for i in selectOptions)
context = assetsInfo.objects.filter(**search_querys)
return context
2019년 7월 18일 목요일 오전 5시 42분 1초 UTC+9, Jani Tiainen 님의 말:
Hi.

You can use dictionary unpacking to kwargs.

.filter(**{"name__contains": "foo", "somefield__gt": 123})



ke 17. heinäk. 2019 klo 23.04 나르엔 <mss...@gmail.com> kirjoitti:
Hello, I am studying django.

When I use objects.filter, I want to search various columns. 

Columns to search for are unknown and have been receiving columns to search through checkbox in html.

What should I do? Please teach me.
It's blocked from below.
selectoptions is name in checkbox.


-------
def get_queryset(self):
        context = super(SearchListView, self).get_queryset()
        query = self.request.GET.get('q')
        selectOptions = self.request.GET.getlist('selectoptions')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Yoo

unread,
Jul 17, 2019, 10:31:07 PM7/17/19
to Django users

나르엔

unread,
Jul 17, 2019, 10:49:20 PM7/17/19
to Django users
I've solved it like this.
Thank you for your help.

def get_queryset(self):
context = super(SearchListView, self).get_queryset()
query = self.request.GET.get('q')
selectOptions = self.request.GET.getlist('selectoptions')
argument_list = []
for field in selectOptions:
argument_list.append( Q(**{field+'__icontains':query}))
context = assetsInfo.objects.filter( reduce(operator.or_, argument_list))
return context

2019년 7월 18일 목요일 오전 11시 31분 7초 UTC+9, Yoo 님의 말:
Reply all
Reply to author
Forward
0 new messages