0

I am using Django-taggit and works fine for me but the exclude has a problem. Keyword is a string like 'key1 key2 key3'. The code is:

keyword = form.cleaned_data['keyword']
qlist = lambda x: [Q(name__icontains=x), Q(author__name__icontains=x),Q(tags__name__icontains=x)]
item_list = Item.objects.distinct()
for key in keyword.split():
    if ('-'==key[0]):
        print 'exclude: %s'%(key[1:])
        item_list = item_list.exclude(reduce(operator.or_,qlist(key[1:])))
     else:
        print 'include: %s'%(key)
        item_list = item_list.filter(reduce(operator.or_,qlist(key)))

It works fine for filter() and for the exclude() Q(name_icontains=x), Q(author_name_icontains=x). But, when I try to use exclude() with Q(tags_name__icontains=x) it doesnt work.

Regards, Cristian

cmaluenda
  • 2,099
  • 1
  • 14
  • 15
  • Maybe the operator.or_ is at fault. See [here](http://stackoverflow.com/questions/8138919/trying-to-reduce-django-q-objects-with-operator-or-seems-to-result-in-reduction) for details. – Laur Ivan Jan 16 '12 at 15:46
  • It is not the problem (I guess), because I run in a django console and execute: `Item.objects.all().exclude(Q(tags__name__icontains=key)).distinct()` And It does the same, show all the element without **exclude** – cmaluenda Jan 16 '12 at 16:38

2 Answers2

0

I'm not really too versed in taggit intricacies, but... Looking at the code, it seems like the "name" is dynamically built in a lazy way.

So, if you're not populating the query explicitly, you're going to get empty request, so Q(tags__name__icontains=key) will be empty, and exclude(...) will just be like filter(not null).

Try to force populating the tag query via a select_related() or something similar.

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
0

I think, It is not supported. I found this link: https://github.com/alex/django-taggit/issues/31

cmaluenda
  • 2,099
  • 1
  • 14
  • 15