52

I'm trying to query a database based on user input tags. The number of tags can be from 0-5, so I need to create the query dynamically.

So I have a tag list, tag_list, and I want to query the database:

design_list = Design.objects.filter(Q(tags__tag__contains = "tag1") and Q(tags__tag__contains = "tag2") and etc. etc. )

How can I create this feature?

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
babbaggeii
  • 7,577
  • 20
  • 64
  • 118

5 Answers5

122

You'll want to loop through the tag_list and apply a filter for each one.

tag_list = ['tag1', 'tag2', 'tag3']
base_qs = Design.objects.all()
for t in tag_list:
    base_qs = base_qs.filter(tags__tag__contains=t)

This will give you results matching all tags, as your example indicated with and. If in fact you needed or instead, you will probably need Q objects.

Edit: I think I have what you're looking for now.

tags = ['tag1', 'tag2', 'tag3']
q_objects = Q() # Create an empty Q object to start with
for t in tags:
    q_objects |= Q(tags__tag__contains=t) # 'or' the Q objects together

designs = Design.objects.filter(q_objects)

I tested this and it seems to work really well.

Edit 2: Credit to kezabelle in #django on Freenode for the initial idea.

Riley Watkins
  • 3,453
  • 3
  • 25
  • 19
  • Thanks, that makes sense. I've tried it with Q objects, but it doesn't return the correct items. Is there anything I'm doing wrong here: ` design_list = Design.objects.all() for t in tag_list: design_list = design_list.filter(Q(tags__tag__contains = t))`. It works when there's only one tag, but no more. – babbaggeii Oct 25 '12 at 20:55
  • In your code there, the Q object is not doing anything. You're simple creating a queryset that, in the end, looks like ``Design.objects.filter(tags__tag__contains='tag1').filter(tags__tag__contains='tag2')``, etc. What you probably want instead is ``Design.objects.filter(Q(tags__tag__contains='tag1') | Q(tags__tag__contains='tag2'))``, but in a way gives you a variable number of Q objects. – Riley Watkins Oct 25 '12 at 21:00
  • Ok, that's what I need to look for. – babbaggeii Oct 25 '12 at 21:04
  • If you want `designs` to be empty when `tags` is empty, you can use `Q(pk__in=[])` as a starting value for `q_objects`. – Jonathan Richards Jan 16 '20 at 03:12
  • It's a shame the QuerySet API doesn't have an "any" method that takes a list of Q objects with "or" semantics, the way filter/exclude/get can take a list of Q objects with "and" semantics. – odigity Sep 03 '22 at 23:05
  • I'm new to this, but I managed to get two examples working: https://gist.github.com/odigity/53bf47cca9a238a905c276760153838d – odigity Sep 03 '22 at 23:44
39

You can use this way:

my_dict = {'field_1': 1, 'field_2': 2, 'field_3': 3, ...}  # Your dict with fields
or_condition = Q()
for key, value in my_dict.items():
    or_condition.add(Q(**{key: value}), Q.OR)

query_set = MyModel.objects.filter(or_condition)

By this way you can use dynamically generated field names. Also you can use Q.AND for AND condition.

Max
  • 607
  • 6
  • 10
  • Is there a way to do this with `__in`? Like to get a Q that matches a field to a list of values, where the name of the field and the list of values are given by variables instead of literals? – Michael Hoffmann Mar 05 '19 at 00:34
  • 4
    @MichaelHoffmann I do not quite understand what you mean, maybe this: Q(**{"{}__in".format(key): value}) – Max Mar 12 '19 at 10:19
3

Just prepare a tag list first then, query like this:

tags = ['tag1', 'tag2',...]
design_list = Design.objects.filter(tags__tag__contains__in = tags)
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
2

You may need to add AND and OR conditions

    query = (Q(fild1='ENABLE'))
    # Filter by list
    query.add(Q(fild2__in=[p.code for p in Objects.field.all()]), Q.AND)

    # filter OR
    q_objects = Q(field3='9999999')
    for x in myList:
        q_objects.add(Q(field3=x.code), Q.OR)

    query.add(q_objects, Q.AND)
1

Use reduce :

from functools import reduce
design_list = Design.objects.filter(reduce(lambda q1,q2: q1 & q2,
                                           [Q(tags__tag__contains=t)
                                            for t in tag_list]))
Eric
  • 4,821
  • 6
  • 33
  • 60